When I run th code, and say input 2.5, the output I get is:
2.5, 4 times "error", and 5.
It means that the computer goes through the catch statement every time, instead of asking for input every time it loops through the while loop and enters the try block.
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
int n = 0;
while(n<5)
{
try
{
int grade = s1.nextInt();
System.out.println(grade);
}
catch(Exception e)
{
System.out.println("error");
}
n++;
}
System.out.println(n);
}
nextInt(). Try printing out the full stack trace in thecatchblock, maybe you'll get some clues.catch(Exception e)...