[英]How to terminate `System.in` keyboard stream when "Enter" key is pressed twice in a row?
For example, the user may enter some input like this into my program:例如,用户可能会在我的程序中输入一些这样的输入:
71 117 48 115 127 125 117 48 121 126 48 96 117 113 115 117
Or like this:或者像这样:
71 117 48
115
127
125 117 48
The user can only terminate the input stream by pressing the "Enter" twice in a row.用户只能通过连续按两次“Enter”来终止输入流。
How can I do this?我怎样才能做到这一点?
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> integers = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
integers.add(scanner.nextInt());
}
}
}
You may want to change how you take input from hasNextInt()
to hasNextLine()
and the same for nextInt()
to nextLine()
您可能希望更改从hasNextInt()
到hasNextLine()
输入方式,并将nextInt()
输入更改为nextLine()
boolean enterOnce = false;
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.isEmpty())
if(enterOnce)
break;
else
enterOnce = true;
}
hasNextInt
and nextInt
ignore white-space; hasNextInt
和nextInt
忽略空格; so you can't use them.所以你不能使用它们。 You could use hasNextLine
and nextLine
(an store the previous line);您可以使用hasNextLine
和nextLine
(存储上一行); and then parse the values from each input line (stopping on two empty lines).然后解析每个输入行的值(停在两个空行上)。 Also, you could use the diamond operator <>
(and I suggest programming to the List
interface (instead of the concrete ArrayList
implementation). Something like此外,您可以使用菱形运算符<>
(我建议对List
接口进行编程(而不是具体的ArrayList
实现)。类似的东西
public static void main(String[] args) {
List<Integer> integers = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String prevLine = "";
String line = null;
while (scanner.hasNextLine()) {
if (line != null) {
prevLine = line;
}
line = scanner.nextLine().trim();
if (line.isEmpty() && prevLine.isEmpty()) {
break;
}
String[] parts = line.split("\\s+");
for (String p : parts) {
integers.add(Integer.parseInt(p));
}
}
}
I found that the previous answers did not behave as required.我发现以前的答案没有按要求行事。 One solution exited the loop after a total of two empty lines instead of after two consecutive empty lines.一种解决方案在总共两个空行之后而不是在两个连续的空行之后退出循环。
2 4
<<ENTER>>
543
<<ENTER>>
---loop breaks---
Another solution exited after a single empty line if that empty line was the first line:如果该空行是第一行,则另一个解决方案在单个空行之后退出:
<<ENTER>>
---loop breaks---
Below, I implement the tracking of consecutive empty lines differently so that these two cases are handled correctly.下面,我以不同的方式实现对连续空行的跟踪,以便正确处理这两种情况。 Furthermore, to prevent InputMismatchExceptions, I also validated each token to be an integer before adding it to the list of integers.此外,为了防止 InputMismatchExceptions,我还在将每个标记添加到整数列表之前验证它是一个整数。
public static void main(String[] args) {
// Initialise list to store the integers from input.
List<Integer> integers = new ArrayList<>();
// Initialise keyboard stream to get integers from keyboard.
Scanner inputStream = new Scanner(System.in);
// Declare a scanner to parse the lines read in from the inputStream.
Scanner lineReader;
// Initialise boolean to track whether two consecutive ENTER keys
// have been pressed.
boolean previousLineEmpty = false;
// Continue taking input from the user and adding the integer input to
// a list until ENTER is pressed twice.
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
// Determine whether the loop should break or add the integers in
// the line to the list.
if (line.isEmpty()) {
// If the current line and previous line are empty,
// ENTER was pressed twice. So break.
if (previousLineEmpty) {
break;
}
// Otherwise, this line is empty and is an empty previous line for
// the next iteration.
else {
previousLineEmpty = true;
}
} else {
// Initialise scanner to process tokens in line.
lineReader = new Scanner(line);
// Process the tokens in the non-empty line, adding integers to the
// list and ignoring non-integers.
while (lineReader.hasNext()) {
// Add token to list if it is an integer.
if (lineReader.hasNextInt()) {
integers.add(lineReader.nextInt());
}
// If token is not an integer, instead advance to next token.
else {
lineReader.next();
}
}
// In the next iteration, this non-empty line is the previous
// line. Set boolean to false.
previousLineEmpty = false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.