Try replacing the row...
while(scanner.hasNextLine())
...with something like this:
while(scanner.hasNextInt())
It's another method of the Scanner class.
- boolean hasNextInt()
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method.
I'm guessing the input you are scanning ends with something other than an int value.