程序在收到Scanner#nextLine()不是”空格”或”回车”时才打印内容。

huangapple go评论84阅读模式
英文:

Program doesn't print anything until Scanner#nextLine() received not a "blank space" nor "enter"

问题

Scanner in = new Scanner(System.in);
while (in.hasNext()) {
    String a = in.nextLine();
    System.out.println(a + " 1");
}

我只想检查当输入为空格或回车时会发生什么,但直到我的输入既不是空格也不是回车时,它才会打印任何内容,就像这样:

a                     // 输入
a 1                   // 输出
                      // 空格(输入)
c                     // 输入
  1                   // 输出
c 1                   // 输出

为什么它要等到读取到既不是空格也不是回车的内容时才打印?而且,当最终打印输出时,它打印了上一行的空格,即 c 1 之前的空格。当我输入 c 时,它给我了 1 和 c 1。

英文:
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
    String a = in.nextLine();
    System.out.println(a + " 1");
}

I just wanna check what will happen when input is SPACE or ENTER, but it doesn't print anything until my input is neither SPACE nor ENTER, like this

a                     // input 
a 1                   // output 
                      // SPACE(input) 
c                     // input
  1                   // output
c 1                   // output

Why doesn't it print until it reads something which is neither BLANK nor SPACE? also, when it finally print, it print the SPACE which is the the line before c 1, when i input c, it give me 1 and c 1.

答案1

得分: 0

>Java Scanner hasNext() vs. hasNextLine():<br>
也就是说,hasNext() 检查输入,并在有另一个 非空白 字符时返回 true。
空白字符 不仅包括空格字符,还包括制表符 (\t),换行符 (\n),甚至更多字符。
连续的空白字符被视为单个分隔符。

System.in 表示 标准输入
当你将 System.in 传递给初始化 Scanner,它将从 标准输入 读取数据。
它将始终等待输入,除非你的输入是 EOF(在 Windows 中是 <kbd>Ctrl</kbd> + <kbd>Z</kbd>,在其他系统中是 <kbd>Ctrl</kbd> + <kbd>D</kbd>)。
因此,扫描器将始终等待来自输入的 非空白 字符。

输入

当你按下 <kbd>Space</kbd> <kbd>Enter</kbd> 时,它会将两个 空白 字符 \n 发送到 标准输入,函数 scanner.hasNext() 仍在等待一个 非空白 字符。并且 scanner.hasNext() 不会返回任何内容。这就是为什么此时没有输出。
然后你按下 <kbd>c</kbd>,它会将 非空白 字符 c 发送到 标准输入

输出

现在你的 标准输入 包含 \n c,第三个字符不是 空白 字符。
最终函数 scanner.hasNext() 返回 true
然后 scanner.nextLine() 读取一行直到字符 \n:它将是 (一个字符),
并且程序打印 1

标准输入 现在变为 c,只有一个字符,
这将再次导致 scanner.hasNext() 返回 true:
扫描器将读取一行,其中将是一个字符 c
并打印 c 1

英文:

>Java Scanner hasNext() vs. hasNextLine():<br>
That is, hasNext() checks the input and returns true if it has another non-whitespace character.
Whitespace includes not only the space character, but also tab space (\t), line feed (\n), and even more characters.
Continuous whitespace characters are treated as a single delimiter.

System.in means Standard Input.
When you pass System.in to init the Scanner, it will read data from Standard Input.
And it will always waiting for the input unless your input is EOF(<kbd>Ctrl</kbd> + <kbd>Z</kbd> in Windows or <kbd>Ctrl</kbd> + <kbd>D</kbd>).
So the scanner will always waiting for a non-whitespace character from input.

Input

When you press <kbd>Space</kbd> <kbd>Enter</kbd>, it sends two whitespace character \n to the Standard Input, and the function scanner.hasNext() is still waiting for a non-whitespace character. And scanner.hasNext() doesn't return anything. That's why there is no output at this time.
Then you press <kbd>c</kbd>, it sends non-whitespace character c to the Standard Input.

Output

Now your Standard Input contains \n c, the third one is not a whitespace character.
Finally the function scanner.hasNext() returns true.
Then the scanner.nextLine() read a line till character \n: it will be (one character),
and program print 1.

Standard Input now becomes c, only one character,
which will cause scanner.hasNext() to return true again:
Scanner will read a line, which will be one character c,
and print c 1.

答案2

得分: -1

如@Savior在这个帖子中所提到的:

> hasNext()用于检查缓冲区中是否有可解析的标记,这些标记由扫描仪的分隔符分隔。
> 由于扫描仪的分隔符是空白字符,而linePattern也是空白字符,因此缓冲区中可能有linePattern,但没有可解析的标记。

考虑使用hasNextLine()

public class Main {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String a = in.nextLine();
            System.out.println(a + " 1");
        }
    }
}
英文:

As mentionned by @Savior from this post :

> hasNext() checks to see if there is a parseable token in the buffer, as separated by the scanner's delimiter.
> Since the scanner's delimiter is whitespace, and the linePattern is also white space, it is possible for there to be a linePattern in the buffer but no parseable tokens.

Consider using hasNextLine()

public class Main {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String a = in.nextLine();
            System.out.println(a + &quot; 1&quot;);
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月9日 23:15:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63814658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定