The code –
for(int i = 0; i < count; i++)
{
System.out.println("Please enter a number!");
try{
numbers[i] = keyboardScanner.nextInt();
}catch(InputMismatchException ex)
{
System.out.println("You did not enter a number!");
}
}
If you enter a String, it loops till ‘count’. Why does that happen ?
Sure, it’ll loop until
count: you caught the exception and continued the loop anyway.You want to
breakout of your loop when you get that exception: