public class ReadInput {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String x = null;
while( (x = input.readLine()) != null ) {
System.out.println(x);
}
}
}
I am able to run this code from the command line by typing 'java ReadInput < input.txt' but not by typing the input directly like 'java ReadInput hello'. When I type 'java ReadInput hello' I seem to get stuck in an infinite loop for some reason. Shouldn't it just work in the same way as typing 'java ReadInput < input.txt' but instead just reprint 'hello'?
When you type
java ReadInput hello
you have not (yet) provided any input toSystem.in
so the program execution blocks until there is something to read. You might notice that the string"hello"
is passed into theargs
parameter to themain()
method.If you're using a debugger (which I strongly recommend), place a breakpoint at the first line of
main()
. You'll notice that, when you run your program asjava ReadInput hello
thatargs[0]
is"hello"
.If you're not using a debugger, you can use
System.out.println()
(this is how beginners frequently debug their code). Add this line of code as the first line ofmain()
:Both of the other two answers above are partially correct, but partially misleading.
Using the following syntax...
...the actual command that is run on the java binary is:
java ReadInput
The operating system shell interprets the
<
sign, and sends the contents of input.txt to the Standard Input Stream (System.in).When you call
System.in.readLine()
, the code checks whether there is a line of input available from the Standard Input Stream. When you piped in a file, this means it takes the next line of the file (as if the OS were taking the contents of the file and typing them in at the prompt). When you run the program without piping a file, it will wait until you, the user, provide it with a line of input from the shell and press the return key.Command-line arguments to the JVM work differently, and do not use the Standard Input Stream (System.in). Ex:
In this case, you are passing in two command-line arguments. These properties will be available through the
args
array:With the code that you provided, the program will end when the result of a readLine() call returns
null
. I believe you may be able to just push enter at the prompt to send a blank line, and end the program. If not, you may have to fix the program to check for an empty string (input.readLine().equals("")).Hope this helps.
Terminate the standard input with
Or set a
special EOF symbol
youself.Arguments given on the program's command line don't go to
System.in
, they go in theargs
array. You could use something likeecho hello | java ReadInput
to run the program, or you could modify the program to to look at itsargs
array and treat it as input. (If you use the latter option, you'd probably want to fall back to reading fromSystem.in
if there's nothing inargs
.)