Under what conditions does Java's Scanner.hasNextLine() block?

前端 未结 3 1079
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 07:19

The javadoc for Scanner.hasNextLine() states:

Returns true if there is another line in the input of this scanner. This method m

3条回答
  •  灰色年华
    2021-02-05 08:03

    It depends on the source that the Scanner gets the input from.

    For example, if it's a file, the entire input is available, so hasNextLine() wouldn't block (since it can know with certainty when the end of the file is reached and there's no more input.

    On the other hand, if the source is standard input, there can always be more input - the user can always type more input - so hasNextLine() would block until the user types in a new line of input.

提交回复
热议问题