英文:
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<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);
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<String> als = new ArrayList<>();
boolean lastLineIsEmpty = false;
while(scanner.hasNextLine()){
String s = scanner.nextLine();
if(s.equals("")) { // 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 ""
, 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.in
的Scanner
。
英文:
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("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);
}
}
By the way, you should not close a Scanner
that wraps System.in
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论