I have a switch
statement with 5 integer options and a default message. I am trying to get it to work so that if anything other than the choice numbers is chosen, an "Invalid Input" message will show up. It currently works for numbers, but if anything else is entered, I get the following exception:
Exception in thread "main" java.util.InputMismatchException
I tried to add a try/catch statement, but it doesn't seem to be working; I keep getting the same error message.
public static void main(String[] args) throws IOException {
int menuItem = -1;
while (menuItem != 0) {
menuItem = menu();
try {
switch (menuItem) {
case 1:
showTaskList();
break;
case 2:
addTask();
break;
case 3:
sortList();
break;
case 4:
deleteTasks();
break;
case 0:
break;
default:
System.out.println("Invalid Input");
break;
}
}
catch (java.util.InputMismatchException err) {
System.out.println("\nINVALID INPUT!");
}
}
}