Originally posted by Jesper de Jong:
So, an InputMismatchException is thrown when the user enters 'q'. If you don't want this, use a method like nextLine() instead, and check if it contains 'q' before converting it to an integer yourself (which you can do with Integer.parseInt()).
Whilst this is true, one of the annoying things about parsing methods like
Integer.parseInt is that you can't look before you leap -- if the next token
is not an int you have to handle this in a catch block instead of in normal code
flow. On the other hand, Scanner has methods like hasNextInt which is true
iff a call to nextInt would succeed at this point, so that you can encorporate
this
test into normal code flow. Try it!