I prepared following piece of code :
public static void main(String[] args) {
Scanner inChoice = new Scanner(System.in);
while(true)
{
try
{
System.out.println("--------------------------------------------------------");
System.out.println("Make your choice :");
System.out.println("For integer - CHOOSE 1");
System.out.println("For floating-point numbers - CHOOSE 2");
intChoice = inChoice.nextInt();
break;
}
catch (InputMismatchException imex)
{
System.out.println("You have made a wrong selection. Try again");
continue;
}
}
}
The problem is when I choose something else then integer number (say 'w' for instance). My intention is to give an opportunity to choose again after exception. But instead my code goes to catch block and loop infinitely and gives me messages :
"--------------------------------------------------------"
"Make your choice :"
"For integer - CHOOSE 1"
"For floating-point numbers - CHOOSE 2"
"You have made a wrong selection. Try again"
"--------------------------------------------------------"
"Make your choice :"
"For integer - CHOOSE 1"
"For floating-point numbers - CHOOSE 2"
"You have made a wrong selection. Try again"
"--------------------------------------------------------"
"Make your choice :"
"For integer - CHOOSE 1"
"For floating-point numbers - CHOOSE 2"
"You have made a wrong selection. Try again"
end so on ...
It don't give me an oportunity to choose again. Can someone explain me please what am I doing wrong ? Thanks