The methods of the scanner will block for more input if there is no current token and the source (in this case System.in
) hasn't reached end-of-stream yet. This will then result in it waiting for your input.
This is also explicitly mentioned in the documentation of hasNext()
(and the other blocking methods):
This method may block while waiting for input to scan.
This can also be seen in the actual implementation of hasNext()
:
public boolean hasNext() {
ensureOpen();
saveState();
modCount++;
while (!sourceClosed) {
if (hasTokenInBuffer()) {
return revertState(true);
}
readInput();
}
boolean result = hasTokenInBuffer();
return revertState(result);
}
As you can see, it will loop as long as the source isn't closed, and if there is no token in the buffer, it will call readInput()
to find the next token. This read will block if there is currently nothing to read from the source at this time, but it hasn't reached end-of-stream/end-of-file yet.