Scanner b = new Scanner(System.in);
public void menu() {
while (true) {
try {
System.out.println("Your options is:");
int option = 0;
option = b.nextInt();
b.nextLine();
if (option == 1)
add();
else if (option == 2)
delete();
else if (option == 3)
print();
else if (option == 4)
nr();
else if (option == 0)
break;
} catch (Exception e) {
System.out.println("Not a valid option!");
// break;
}
}
b.close();
}
Thing is that whenever I'm giving something else than the selected options(0,1,2,3,4) the function prints the message from the catch and then asks for a new option but this thing is done repeatedly. If the break is there, the whole execution is stopped and all I want is to ask again for an option but without the infinite loop. For example, if I type a "r" instead of a number, it should catch the exception and get again through the while.
What am I missing?