Im trying to catch an InputMismatchException it works at the first interraction , but when the menu() method is called again , it starts looping until it gets stuck in an error.
In the try catch my objective was to get an error message and after that start the menu() method again.
I have the following code:
public class Menu extends ProfilesManager {
static Scanner sc = new Scanner(System.in);
public static void menu() {
int number;
System.out.println("** Welcome... **\n ");
System.out.println("* what you wanna do?:\n");
System.out.println("(1) Login \n(2) Register \n(3) Find User \n(4) Exit\n");
System.out.print("-Answer?: ");
try {
number = sc.nextInt();
if (number == 1) {
Login();
} else if (number == 2) {
Register();
} else if (number == 3) {
FindUser();
} else if (number== 4) {
Exit();
}
} catch (InputMismatchException e) {
System.out.println("Error , only numbers!!");
menu();
}
}
}
}
menuPrinc();? If you want to restart something, use an actual loop, not recursionmenu()in thecatchblock