Below is an example of a switch statement with 3 cases and a default based on an int. I am wondering how I can have the default return the message "Error: Please Retry" and then loop again if something other than an int is entered? Currently if you enter any int, the program works fine. However, if you enter any other character besides an int - the build fails. Do I need to set up another case and then build another class to deal with this?
Thanks in advance.
int choice;
while(true){
System.out.println("Please enter the number of your choice:");
System.out.println("1. Monitor A");
System.out.println("2. Monitor B");
System.out.println("3. Exit");
choice = scanner.nextInt();
switch(choice){
case 1:
monitorA();
break;
case 2:
monitorB();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Error: Please Retry");
break;
}
}