Why is hasNext() False, but hasNextLine() is True?

后端 未结 5 2094
情歌与酒
情歌与酒 2020-12-03 06:21

Question

How is it that for a scanner object the hasNextLine() method returns true while the hasNext() method returns false?

Not

5条回答
  •  甜味超标
    2020-12-03 07:02

    You are consuming the value of next(), but asking for hasNext() and hasNextLine(). next(), per default, returns everything to the next whitespace(). So you are iterating through all whitespace seperated strings, and after each of them you are asking about the nextLine().

    i 1 1 -> hasNextLine()? True. hasNext()? Also true.

    1 1 -> hasNextLine()? True. hasNext()? Also true (still a whitespace left)

    1 -> hasNextLine()? True (Line Seperator, probably). haxNext? False, no whitespace anymore.

提交回复
热议问题