Home > Mobile >  Java Menu Program "X" to exit switch
Java Menu Program "X" to exit switch

Time:12-12

I have a java program that uses a switch structure and loop to continue while user inputs a value of 1 - 5 and if "x", the program exits. I keep erroring on " choice = input.next().charAt(0);" and " switch (choice) {" I am very new to coding so I apologize if its a silly question. Here is the full code:

import java.util.Scanner;
    public class Menu {
     public static void main(String args[]) {
Scanner sc = new Scanner(System.in); 
    char c = sc.next().charAt(0);

    while (true)  {
System.out.println("Java Help Menu \n 1. Option 1 \n 2. Option 2 \n 3. Option 3 \n 4. Option 4 \n 5. Option 5 \n x. Exit \n");        
    choice = input.next().charAt(0);
        switch (choice) {
        case '1':
            System.out.println("Selection: 1 \n");
            break;
        case '2':
            System.out.println("Selection: 2 \n");
            break;
        case '3':
            System.out.println("Selection: 3 \n");
            break;
        case '4':
            System.out.println("Selection: 4 \n");
            break;
        case '5':
            System.out.println("Selection: 5 \n");
            break;
        case 'x':
            System.out.println("Thank you. Good bye.");
        }
    } 
}

}

CodePudding user response:

There are some errors in your code

  1. choice was not declared

  2. you mix char and int

  3. input must be sc

  4. you read a charachter before the loop, which is not needed

    
    public class Menu {
        public static void main(String args[]) {
            Scanner sc = new Scanner(System.in);
    
            while (true) {
                System.out.println(
                        "Java Help Menu \n 1. Option 1 \n 2. Option 2 \n 3. Option 3 \n 4. Option 4 \n 5. Option 5 \n x. Exit \n");
                char choice = sc.next().charAt(0);
                switch (choice) {
                case '1':
                    System.out.println("Selection: 1 \n");
                    break;
                case '2':
                    System.out.println("Selection: 2 \n");
                    break;
                case '3':
                    System.out.println("Selection: 3 \n");
                    break;
                case '4':
                    System.out.println("Selection: 4 \n");
                    break;
                case '5':
                    System.out.println("Selection: 5 \n");
                    break;
                case 'x':
                    System.out.println("Thank you. Good bye.");
                }
            }
        }
    }
    

CodePudding user response:

You can't mix types in switch statement the only reason this is compiling is that your character 'x' is getting implicitly converted to int value of the ascii table. So in order to meet this condition user needs to enter 120 as the value since that is the value in the ascii table for lowercase x

To make this program work how you intended use String as your input and check for "1" "2" ... "x"

  • Related