use finally
after catch
block and put the following two statements in it like this :
try {
} catch(InputMismatchException iox) {
} finally {
System.out.print("Answer (y/n): ");
answer = scan.next();
}
Note : When exception occurs before System.out.print("Answer (y/n): ");
and
answer = scan.next();
statements,these two statements are not executed.But the statements within the finally
block executes regardless of what happens within the try
block.So,if you use the two statements in finally
block these will always be executed infinite looping won't happen.