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