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.
java java.util.scanner stdin
dmoench
source share