I have two groups of data I need to take from the user of an unknown size, and I would like it if the user could specify the end of their input with Ctrl-D (Ctrl-Z in Windows I believe).
This works flawlessly for the first group, but the second group reads nothing. In fact, after EOF is sent to the console, I'm unable to read from System.in
at all for the remainder of the program (readLine()
will always return null).
How do I effectively "reopen" System.in
in order to read more inputs after an EOF input?
Here is some example code to express what I'm trying to accomplish:
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
String line;
System.out.println("Enter first inputs (Ctrl-D to close)");
while ((line = systemIn.readLine()) != null) {
System.out.printf("Received input %s%n", line);
}
System.out.println("First inputs have been received!\n");
// What do I put here to use System.in again?
System.out.println("Enter second inputs (Ctrl-D to close)");
while ((line = systemIn.readLine()) != null) {
System.out.printf("Received input %s%n", line);
}
System.out.println("Second inputs have been received!\n");
}
}