How to stop reading multiple lines from stdin using Scanner?

I am working on a JAVA assignment, should handle multiple lines of input. The instructions read "Input is read from stdin."

An example of sample input is given:

one 1 two 2 three 3 

I don’t understand what the “read from stdin” input example above means.

Here's the test program I wrote isolates my confusion:

 import java.io.*; import java.util.Scanner; class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); while(stdin.hasNextLine()) { String line = stdin.nextLine(); String[] tokens = line.split(" "); System.out.println(Integer.parseInt(tokens[1])); } } 

When I run this program in the console, it waits for input and every time I enter a line, it returns it back, as I expected. So I thought that perhaps the input pattern above would be achieved by entering each of the three lines in this way. However, there seems to be no way to end this process. After entering three lines, how can I complete the login? I tried just pressing the key twice, but it looks like a line consisting only of a newline character, which causes an error, because the line does not match the format of the 2nd format that it expects.

Here, the console interaction is as follows:

 javac Test.java java Test one 1 1 two 2 2 three 3 3 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Test.main(Test.java:13) 

I would be grateful for any help in indicating a gap in my understanding.

+8
java java.util.scanner stdin
source share
4 answers

You can try asking for blank entries

 import java.util.Scanner; public class Test { public static void main(String[] args) { String line; Scanner stdin = new Scanner(System.in); while(stdin.hasNextLine() && !( line = stdin.nextLine() ).equals( "" )) { String[] tokens = line.split(" "); System.out.println(Integer.parseInt(tokens[1])); } stdin.close(); } } 
  • Your code is almost complete. All you have to do is exit the while loop. In this code example, I added a condition to it, which first sets the read value for the string and, secondly, checks the returned string if it is empty; if so, the second condition of the while loop returns false and allows it to stop.
  • You will get an exception to the array index from the exclusion limits only if you do not enter at least two values ​​marked with a space. If you do not try to get the second value> token [1] <by a static index, you could avoid this error.
  • When you use readers, keep in mind to close them after use.
  • And last but not least, have you tried the usual Ctrl + C key combination to end processes in consoles?

Good luck

+4
source share

You can also put your values ​​in a file, for example. input.txt and execute:

 java Test < input.txt 
+2
source share

From the shell, press Ctrl-D and it will close stdin. Alternatively, the pipe input to

 cat your-input-file | java Test 
+1
source share

To stop the entry, you can either ask the user to enter quit to exit, and then check the presence of this line in the input, exit the loop when it detects, or use the counter in the loop, the exit loop, when the maximum iterations are reached. The break statement takes you out of the loop.

+1
source share

All Articles