I'm just trying to figure out how to implement Exception Handling in a Switch Statement which will then loop me back to the menu for input again. What I am trying to catch is String input where it should be Integer. I think I'm close but can't find any help in previous questions here, the output I'm getting when I enter a String is a neverending "Please enter an integer". Can someone please point out where I'm going wrong? Thanks.
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("\n*** Please make a selection from 1 to 3 and press ENTER: ***\n");
System.out.println("1. Test1");
System.out.println("2. Test2");
System.out.println("3. Test3");
boolean continueLoop = true;
do
{
try
{
int selection = input.nextInt();
if(selection <=0 || selection >= 3)
{
System.out.println("\nInvalid Selection - Please Try Again!\n");
}
switch(selection)
{
case 1:
System.out.println("Test1");
continueLoop = false;
break;
case 2:
System.out.println("Test2");
continueLoop = false;
break;
case 3:
System.out.println("Test3");
continueLoop = false;
break;
default:
continueLoop = false;
}//end switch
}//end try
catch(InputMismatchException inputMismatchException)
{
System.out.println("Please enter an integer");
}
}while ( continueLoop );
}
}