adevapp adevapp - 8 months ago 40
Java Question

User input if statements in Java not working

i am using scanner to take user input.

Scanner scanner = new Scanner(;

Int choice = scanner.nextInt();

if(choice==1) {
System.out.println(" you chosed number 1!")

if(choice==2) {
System.out.println(" you chosed number 2!")

if(choice==3) {
System.out.println(" you chosed number 3!")

if(choice==q) {
System.out.println("the game will end now!");

so when you enter the game you have a menu that pops up.
You can chose between 1, 2, 3 or q.
if you press any of the numbers the game will take you to those sections.
but if you press q the game will end.

I don't know how to fix so that i can enter q and game ends.


You've got some typos, and your logic is incorrect. You are missing quite a lot of semicolons, they end a statement. When will an int become a char? The char isn't enclosed it in quotes. Use next to get a string version. Use .equals() to check strings.

Scanner scanner = new Scanner(;

String choice =;

if(choice.equals("1")) {
    System.out.println(" you chose number 1!");
} else if(choice.equals("2")) {
    System.out.println(" you chose number 2!");
} else if(choice.equals("3")) {
    System.out.println(" you chose number 3!");
} else if(choice.equalsIgnoreCase("q")) {
    System.out.println("the game will end now!");

You can also parse as an integer and compare using ==.

The above code will get the user input as a string. Then it will check if it equals 1, 2, 3, or q and then executes lines accordingly. Q is checked and case is ignored.