When I was tangling my java programming assignment during my school years, I came across, once upon a time, a tiny yet disturbing problem caused by java.util.Scanner.
Everything was fine when I manually test my program but it broke down as soon as I compiled it with automatically test file.
The error is NoSuchElementException at Scanner.
What the hell? What's wrong? I was pretty much confused.
And after searching and searching and searching on the Internet I finally got the answer:
In automatic test, the test file was always read in with the same file stream, and was always passed to the first scanner it met.
So what if we close the System.in filestream and try starting anothing one?
Haha, no it's not gonna be working as this operation of closing filestream is irreversible.
And here I got the solution:
Let the scanner be final static.
final static Scanner sc = new Scanner(System.in);
By this way, whenever we call an instance of a scanner, it is always a new one.
Hope this may help you bros~