When you enter a token which is not a number, calling nextInt does not move the scanner beyond the token. As a result, it's just reading the same token over and over.
Here's the documentation for nextInt: http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextInt%28%29 (although the relevant behavior is actually described in the version which takes an argument, here: http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextInt%28int%29 )
So, to give you a quick example, let's assume that the input string is "1 2 Foo"
. I will add an X to show where the Scanner is currently positioned. It starts out "X1 2 Foo"
. Then the first call to input.nextInt()
happens. It returns 1 and now we're at "1 X2 Foo"
. Then the second call to input.nextInt()
happens. It returns 2 and now we're at "1 2 XFoo"
. Then the third call to input.nextInt()
happens. It doesn't return, but rather throws an InputMismatchException and it also doesn't advance the Scanner. So we're still at "1 2 XFoo"
. So when we call it a fourth or fifth time, the same thing happens.
To solve this problem, an input.next()
call will consume the next token, whatever it is. After this call, in our example, we would be at "1 2 FooX"
.
Note, however, that the call to input.next()
can also still throw exceptions. Specifically, it can throw IllegalStateException if the Scanner is closed (which shouldn't happen in your code) or NoSuchElementException if the Scanner has reached the end of input (which won't happen if you're reading in from the keyboard, as I assume that you are). But just to be safe, you should either catch those errors and break out of the loop or not catch them and instead declare them as thrown by your method.
input
in this context? – templatetypedefException
is very bad, don't do it. It could be aNullPointerException
but you would never know. It would also produce the results you are describing. – unholysampler