#java

1 messages · Page 1 of 1 (latest)

cunning wave
#

hello how do i implement a menu system

night widgetBOT
#

<@&987246399047479336> please have a look, thanks.

cunning wave
#

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

pine moat
#

As a commandline, graphical user interface, ... ?

cunning wave
#

im jus confused now

pine moat
#

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, ...

cunning wave
#

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

crisp jolt
#

do you do it in console?

cunning wave
#

huh

#

laptop

pine moat
#

No. As in what kind of application.

#

Is it with a user interface, or command line?

cunning wave
#

what does that meannnn

#

apache netbeans is what i use

crisp jolt
#

bro what

#

how do you run the app?

#

in netbeans i mean

#

where does the things appear

#

send a screenshot

cunning wave
#

u mean like dis

pine moat
#

Ok, in netbeans. How are you showing your information? Are you using System.out.println or some graphical library?

cunning wave
#

?

#

this is what ive done so far

pine moat
#

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:

night widgetBOT
#

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.

cunning wave
#

i just print each

crisp jolt
#

what you should have is have an infnite loop

#

which will take an input

pine moat
#

Well you just need to implement the rest of the input options, but it's not that complex.

crisp jolt
#

you also print sort of "help menu"

cunning wave
#

hmmm

crisp jolt
#

something like this:

  1. View account
  2. Deposit Money
  3. Withdraw Money
  4. ...
    Please select an option: take input with scanner
#

then have some sort of switch statement

cunning wave
#

then i use if?

crisp jolt
#

do you know whats switch?

cunning wave
#

nope

#

sry

crisp jolt
#

okay well then we can use if

#

you take number as an input

#

and then do the code according to the number in input

cunning wave
#

alright imma try now

crisp jolt
#

so if you enter 1, it will write code with account info

cunning wave
#

and for the program to stop if user chooses option 5

#

i use break;

#

??

crisp jolt
#

yes 👍

#

im also kinda confused on why your theme looks same as intellijs

#

r u sure u use netbeans?

cunning wave
#

my teacher downloaded it

crisp jolt
#

can you show me full screen screenshot perphaps? (just out of curiosity lol)

#

no worries if you cant

cunning wave
crisp jolt
#

oh thats cool

cunning wave
#

btw im doing it wrong

#

arent i

crisp jolt
#

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
  }
}
night widgetBOT
cunning wave
#

do i need the public static thing again?

crisp jolt
#

no

cunning wave
#

i alr have a scanner

#

do i need a new one

crisp jolt
#

no

cunning wave
#

so i dont have to delete anything i just do the while loop?

crisp jolt
#

yep

cunning wave
#

how do i tell it when user enters a number

#

print this

crisp jolt
#

well it waits for the input

cunning wave
#

i cant send images

#

whaaa

crisp jolt
#

youre writing the while loop wrong

cunning wave
#

Sorry if im bothering but i literally dont know anything

crisp jolt
# cunning wave

you literally wrote the while loop on the top here correctly...

cunning wave
#

ok i fixed it @crisp jolt

#

but now the program isnt stopping after i enter 1

surreal grove
#

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