如何使用 Java Scanner 测试空行?

发布于 2024-12-02 22:48:20 字数 157 浏览 0 评论 0原文

我期待用扫描仪输入,直到没有任何内容(即当用户输入空行时)。我该如何实现这一目标?

我尝试过:

while (scanner.hasNext()) {
    // process input
}

但这会让我陷入困境

I am expecting input with the scanner until there is nothing (i.e. when user enters a blank line). How do I achieve this?

I tried:

while (scanner.hasNext()) {
    // process input
}

But that will get me stuck in the loop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情释 2024-12-09 22:48:20

这是一个方法:

Scanner keyboard = new Scanner(System.in);
String line = null;
while(!(line = keyboard.nextLine()).isEmpty()) {
  String[] values = line.split("\\s+");
  System.out.print("entered: " + Arrays.toString(values) + "\n");
}
System.out.print("Bye!");

Here's a way:

Scanner keyboard = new Scanner(System.in);
String line = null;
while(!(line = keyboard.nextLine()).isEmpty()) {
  String[] values = line.split("\\s+");
  System.out.print("entered: " + Arrays.toString(values) + "\n");
}
System.out.print("Bye!");
堇色安年 2024-12-09 22:48:20

来自 http://www.java-made-easy.com/java-扫描仪帮助.html:

问:如果我用 Java 的 Scanner 扫描空行会发生什么?

答:这要看情况。如果您使用 nextLine(),则会读取空白行
作为一个空字符串。这意味着如果您要存储空白
在字符串变量中,该变量将保存“”。它不会
存储“ ”或放置多个空格。如果您使用 next(),
那么它根本不会读取空行。它们被完全跳过。

我的猜测是 nextLine() 仍会在空行上触发,因为从技术上讲,扫描仪将具有空字符串 ""。因此,您可以检查 if s.nextLine().equals("")

From http://www.java-made-easy.com/java-scanner-help.html:

Q: What happens if I scan a blank line with Java's Scanner?

A: It depends. If you're using nextLine(), a blank line will be read
in as an empty String. This means that if you were to store the blank
line in a String variable, the variable would hold "". It will NOT
store " " or however many spaces were placed. If you're using next(),
then it will not read blank lines at all. They are completely skipped.

My guess is that nextLine() will still trigger on a blank line, since technically the Scanner will have the empty String "". So, you could check if s.nextLine().equals("")

浅语花开 2024-12-09 22:48:20

使用 scanner.nextLine() 的建议的问题在于它实际上以 String 形式返回下一行。这意味着那里的任何文本都会被消耗。如果您有兴趣扫描该行的内容……那么,太糟糕了!您必须自己解析返回的String 内容。

更好的方法是使用

while (scanner.findInLine("(?=\\S)") != null) {
    // Process the line here…
    …

    // After processing this line, advance to the next line (unless at EOF)
    if (scanner.hasNextLine()) {
        scanner.nextLine();
    } else {
        break;
    }
}

由于 (?=\S) 是零宽度先行断言,因此它永远不会消耗任何输入。如果它在当前行中找到任何非空白文本,它将执行循环体。

如果您确定循环体已经消耗了该行中的所有非空白文本,则可以省略 else break;

The problem with the suggestions to use scanner.nextLine() is that it actually returns the next line as a String. That means that any text that is there gets consumed. If you are interested in scanning the contents of that line… well, too bad! You would have to parse the contents of the returned String yourself.

A better way would be to use

while (scanner.findInLine("(?=\\S)") != null) {
    // Process the line here…
    …

    // After processing this line, advance to the next line (unless at EOF)
    if (scanner.hasNextLine()) {
        scanner.nextLine();
    } else {
        break;
    }
}

Since (?=\S) is a zero-width lookahead assertion, it will never consume any input. If it finds any non-whitespace text in the current line, it will execute the loop body.

You could omit the else break; if you are certain that the loop body will have consumed all non-whitespace text in that line already.

美男兮 2024-12-09 22:48:20
Scanner key = new Scanner(new File("data.txt"));
String data = "";

while(key.hasNextLine()){
    String nextLine = key.nextLine();

    data += nextLine.equals("") ? "\n" :nextLine;

}
System.out.println(data);
Scanner key = new Scanner(new File("data.txt"));
String data = "";

while(key.hasNextLine()){
    String nextLine = key.nextLine();

    data += nextLine.equals("") ? "\n" :nextLine;

}
System.out.println(data);
云巢 2024-12-09 22:48:20

AlexFZ 是对的,scanner.hasNext() 将始终为 true 并且循环不会结束,因为即使它是空的“”,也总是有字符串输入。

我有同样的问题,我是这样解决的:

do{
    // process input
}while(line.length()!=0);

I think do-while will fit here better becasue you have to evaluate input after user has entered it.

AlexFZ is right, scanner.hasNext() will always be true and loop doesn't end, because there is always string input even though it is empty "".

I had a same problem and i solved it like this:

do{
    // process input
}while(line.length()!=0);

I think do-while will fit here better becasue you have to evaluate input after user has entered it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文