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.
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()
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; so you can't use them. You could use hasNextLine
and nextLine
(an store the previous line); 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
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.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.