0

I'm trying to make a menu in Java and get user input until the user enters the right input, so I used while() in my code. But when I run my code, the only thing that runs is while loop and it catches the error but it just stops there.

Here is my code so far:

public static void main(String[] args) {

    // calling void choice
    choice();
    // System.out.println("");
    int choice = checkValidMenu();
    while (choice != 0) {
        // System.out.println("");
        choice = input.nextInt();
        if (choice == 1) {
            System.out.println("Enter 1 to validate an integer: ");
            choice = input.nextInt();
        } // choice 1
        else if (choice == 2) {
            System.out.println("Enter 2 to validate a decimal number: ");
            choice = input.nextInt();
        } // choice 2
        else if (choice == 3) {
            System.out.println("Enter 3 to process an array: ");
            choice = input.nextInt();
        } // choice 3
        else if (choice == 4) {
            System.out.println("Enter 4 to process a file: ");
            choice = input.nextInt();
        } // choice
        else if (choice == 5) {
            System.out.println("Enter 5 to exit:");
            choice = input.nextInt();
        } // choice5
    } // menu while

}// main

//method for choice
public static void choice() {
    System.out.println("\tMenu Options");
    System.out.println("Enter 1 to validate an integer: ");
    System.out.println("Enter 2 to validate a decimal choice: ");
    System.out.println("Enter 3 to process an array: ");
    System.out.println("Enter 4 to process a file");
    System.out.println("Enter 5 to exit: ");
}// void method */
    /// creating a method to validate user input

public static int checkValidMenu() {
    try {
        return input.nextInt();
    } catch (InputMismatchException e) {
        System.out.println("*** Invalid entry - Try again");
        input.next();

        return 0;
    }
}

}// class
rensothearin
  • 530
  • 1
  • 3
  • 15

2 Answers2

0

When you catch the error, you return 0, which is assigned to "choice". So you enter the while loop with choice 0; maybe you should not return 0 when catching the invalid entry? I'm not sure if I understand your point.

user20191130
  • 162
  • 7
0

In your main method, comment the choice=input.nextint() after while loop line. You can add more inputs to make your question clear though.

    public static void main(String[] args) {

        // calling void choice
        choice();
        
        int choice = checkValidMenu();
        while (choice != 0) {
            
            //choice = input.nextInt();
            if (choice == 1) {
                System.out.println("Enter 1 to validate an integer: ");
                choice = input.nextInt();
            } 
            else if (choice == 2) {
                System.out.println("Enter 2 to validate a decimal number: ");
                choice = input.nextInt();
            } 
            else if (choice == 3) {
                System.out.println("Enter 3 to process an array: ");
                choice = input.nextInt();
            } 
            else if (choice == 4) {
                System.out.println("Enter 4 to process a file: ");
                choice = input.nextInt();
            } 
            else if (choice == 5) {
                System.out.println("Enter 5 to exit:");
                choice = input.nextInt();
            } 
        } 

    }
Ashish
  • 303
  • 5
  • 20