使用扫描器调试 Eclipse 控制台

Debugging eclipse console with scanner

我正在尝试使用以下示例代码和示例输入从 eclipse 控制台读取输入。在我 运行 程序之后,我将整个输入粘贴到控制台中。 示例代码

public class App {
    private static Scanner scanner;

    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        scanner = scanner.useDelimiter("\n");

        while (scanner.hasNextLine()) {
            String out = scanner.nextLine();
            System.out.println(out);
            // System.exit(0);
        }

        scanner.close();
        System.out.close();
        System.exit(0);
    }
}

示例输入:

8 5  
2 3 1 2 3 2 3 3  
0 3  
4 6  
6 7  
3 5  
0 7  

我的问题是 1.) 除最后一行外,所有内容均已打印。如果我按回车键,最后一行在换行符后打印,与输入不同。 2.) 无法退出循环并终止 JVM。与 eclipse luna jre8/jre7

  1. 最后一行只有在您按下回车键后才会被读取。我认为 Eclipse 甚至不会以其他方式将字符发送到您的应用程序(例如,您可以删除您输入的内容)。
  2. Scanner#hasNextLine may block. So it will wait for you next input. You may have to provide something like an EOF character 终止循环,但我不确定您是否可以将其输入 Eclipse 控制台。

作为解决方法,您可以使用 'quit' 这样的输入来退出循环,或者如果您确实从文件中读取,那么它总是有一个 EOF。

输入的最后一行 0 7 没有“/n”"next line"。在下一行输入的 Scanner API 中,每个 '/n' 或换行符都是 horizon 。最后一行没有 horizon。所以 "findPatternInBuffer" 方法匹配模式直到 horizon 如果 horizon 没有找到并且没有下一行然后它等待就好像有更多可能的输入模式。扫描仪的第 1013-1016 行 class ->

// The match may be longer if didn't hit horizon or real end
if (searchLimit != horizonLimit) {
     // Hit an artificial end; try to extend the match
    needinput = true;

如果您在输入中提供一个空的下一行,问题就会消失。