扫描器与未知行数的输入

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

Scanner with unknown lines of input

问题

我在使用Scanner时遇到了一个问题即如何让我的程序知道我何时完成输入以下代码在我提供输入并在IDE终端中按两次回车后可以用于单个段落

Scanner scanner = new Scanner(System.in);
ArrayList<String> als = new ArrayList<>();
while(scanner.hasNextLine()){
    String s = scanner.nextLine();
    if(s.equals("")) break; 
    als.add(s);
}
scanner.close();
for(String ss : als) System.out.println(ss);
然而我希望能够提供多个段落的输入

word word word word
word word word word

word word word word
word word word word

然后将它们全部存储在ArrayList中并进行打印
英文:

I have issue letting my program know when I'm done supplying input using Scanner. The following code works for a single paragraph after I supply the input and press enter twice in my IDE terminal.

            Scanner scanner = new Scanner(System.in);
            ArrayList&lt;String&gt; als = new ArrayList&lt;&gt;();
            while(scanner.hasNextLine()){
                String s = scanner.nextLine();
                if(s.equals(&quot;&quot;)) break; 
                als.add(s);
            }
            scanner.close();
            for(String ss : als) System.out.println(ss);

I would however like to supply multiple paragraphs of input ie.

word word word word
word word word word

word word word word
word word word word

and store them all in the arraylist then print it.

答案1

得分: 1

>我想,当只有空行时,是否有办法在两行""后停止它?

在这种情况下,您可以添加一个boolean变量,用于指示最后一行是否为空:

Scanner scanner = new Scanner(System.in);
ArrayList<String> als = new ArrayList<>();
boolean lastLineIsEmpty = false;
while(scanner.hasNextLine()){
    String s = scanner.nextLine();
    if(s.equals("")) { // 这一行是空的
        if (lastLineIsEmpty) { // 上一行也是空的
            break;
        } else { // 上一行不是空的,将变量设置为true
            lastLineIsEmpty = true;
        }
    } else { // 这一行不是空的,重置变量
        lastLineIsEmpty = false;
    }
    als.add(s);
}
for(String ss : als) System.out.println(ss);

如果您希望在出现 N"" 后停止,您可以使用一个int变量,该变量计算扫描器已经看到了多少个连续的空行。

英文:

> I suppose when there are only empty lines coming through. Is there a way to make it stop after two lines of ""?

In that case, you can add a boolean variable that indicates whether the last line is empty:

    Scanner scanner = new Scanner(System.in);
    ArrayList&lt;String&gt; als = new ArrayList&lt;&gt;();
    boolean lastLineIsEmpty = false;
    while(scanner.hasNextLine()){
        String s = scanner.nextLine();
        if(s.equals(&quot;&quot;)) { // this line is empty
            if (lastLineIsEmpty) { // last line is also empty
                break;
            } else { // last line is not empty, set the variable to true
                lastLineIsEmpty = true;
            }
        } else { // this line is not empty, reset the variable
            lastLineIsEmpty = false;
        }
        als.add(s);
    }
    for(String ss : als) System.out.println(ss);

If you want it to stop after N lines of &quot;&quot;, you can instead use an int variable, that counts how many consecutive empty lines the scanner has seen.

答案2

得分: 1

你在你的问题的评论中写道:

> 有没有办法在两行""后让它停止?

只需要添加一个计数器来计算连续输入的空行数量。一旦计数器大于一,你就已经到达输入的末尾。

import java.util.ArrayList;
import java.util.Scanner;

public class Paragraf {

    public static void main(String[] args) {

        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);

        ArrayList<String> als = new ArrayList<>();
        int count = 0;
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            if (s.isEmpty()) {
                count++;
                if (count > 1) {
                    break;
                }
            }
            else {
                count = 0;
            }
            als.add(s);
        }
        als.forEach(System.out::println);
    }
}

顺便说一下,不应该关闭包装了System.inScanner

英文:

You wrote in your comment to your question:

> Is there a way to make it stop after two lines of ""?

Just add a counter that counts the number of consecutive blank lines entered. Once the counter is greater than one, you have reached the end of the input.

import java.util.ArrayList;
import java.util.Scanner;

public class Paragraf {

    public static void main(String[] args) {

        @SuppressWarnings(&quot;resource&quot;)
        Scanner scanner = new Scanner(System.in);

        ArrayList&lt;String&gt; als = new ArrayList&lt;&gt;();
        int count = 0;
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            if (s.isEmpty()) {
                count++;
                if (count &gt; 1) {
                    break;
                }
            }
            else {
                count = 0;
            }
            als.add(s);
        }
        als.forEach(System.out::println);
    }
}

By the way, you should not close a Scanner that wraps System.in.

huangapple
  • 本文由 发表于 2020年9月29日 21:30:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64120669.html
匿名

发表评论

匿名网友

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

确定