#java
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Implement a menu system to allow the user to perform the following operations:
• View Account: Display the user's name and balance.
• Deposit Money: Prompt the user to enter an amount to deposit. Update the balance
accordingly.
• Withdraw Money: Prompt the user to enter an amount to withdraw. Ensure that there
are sufficient funds before performing the withdrawal.
• Check Balance: Display the current balance
i do not know how
its part of my hw
As a commandline, graphical user interface, ... ?
wym
im jus confused now
You say you want to implement a menu system, but what kind?
You could use something commandline using the scanner, a graphical one using swing/javafx, ...
im new
so sorry if i dont understand
but if u mean what we use in uni
its netbeans
ive done half of the assignment
by adding the username
and stuff
but im stuck
and cant find anything useful on the internet
do you do it in console?
bro what
how do you run the app?
in netbeans i mean
where does the things appear
send a screenshot
Ok, in netbeans. How are you showing your information? Are you using System.out.println or some graphical library?
Ok, so command line.
Now what's the issue with creating a menu? You print their options, and ask them to select an item.
And also read this:
Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.
To prevent this, when reading user input, always only use nextLine. If you need an int, do
int value = Integer.parseInt(scanner.nextLine());
instead of using nextInt.
Assume the following:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:
Hello , you are 20 years old.
The reason why is that when you hit the enter button, your actual input is
20\n
and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.
So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.
so i dont need no other code?
i just print each
Well you just need to implement the rest of the input options, but it's not that complex.
you also print sort of "help menu"
hmmm
something like this:
- View account
- Deposit Money
- Withdraw Money
- ...
Please select an option: take input with scanner
then have some sort of switch statement
then i use if?
do you know whats switch?
okay well then we can use if
you take number as an input
and then do the code according to the number in input
alright imma try now
so if you enter 1, it will write code with account info
yes 👍
im also kinda confused on why your theme looks same as intellijs
r u sure u use netbeans?
can you show me full screen screenshot perphaps? (just out of curiosity lol)
no worries if you cant
oh thats cool
let me help you start out
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (true) {
int option = sc.nextInt();
// Do ifs to check which option was selected
}
}
Detected code, here are some useful tools:
do i need the public static thing again?
no
no
so i dont have to delete anything i just do the while loop?
yep
well it waits for the input
youre writing the while loop wrong
you literally wrote the while loop on the top here correctly...
replace while for if
since the condition stays true, is infinite
Also, since this has an exit
I expect it to show the menu until you enter 5
so put the menu in a while(true)
there evaluate if you chose 5, a break; to stop the loop