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

后端 未结 5 2092
情歌与酒
情歌与酒 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 06:56

    The Basic concept of hasNext() and hasNextLine() is

    hasNextLine:- Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.

    Returns: true if and only if this scanner has another line of input Throws: IllegalStateException - if this scanner is closed

    hasNext

    Returns true if the next complete token matches the specified pattern.

    A complete token is prefixed and postfixed by input that matches the delimiter pattern. This method may block while waiting for input. The scanner does not advance past any input.

    Parameters: pattern - the pattern to scan for

    Returns: true if and only if this scanner has another token matching the specified pattern

    Since your last input saying true for nextLine() because A call to scan.nextLine(); returns the next token. It's important to note that the scanner returns a space and a letter, because it's reading from the end of the last token until the beginning of the next line.

提交回复
热议问题