You are viewing a single comment's thread. Return to all comments →
how to terminate the loop?
When scan.hasNext() == false.
how can that condition be met in the given problem set??
please if anyone can explain.
I'm 20 days late, but the reason is: hasNext() checks if there is a string remaining in the Scanner; and returns false only when nothing is left there, which is equivalent to EOF
But the input is being given line by line. How can you say it will search for EOF? It keeps on asking for input!!!
I had also the same question, I could not find any easy explenation over net.
You can use a while loop to look for it
If there is no next line it will terminate the loop
I've done some research on this.
Basically, hasNext() will block until Standard input (stdin) is closed.
So when you run in an IDE, this loop never ends if you never kill stdin.
It looks like on Hackerrank, at the end of the file, stdin is being closed thus it ends.
You can also have some kind of 'END_OF_FILE' token to indicate end of input.
if it reached the end of file then
I had the very same problem. I didn´t had the idea to put scanner.hasNext()==true in the termination condition.