Karl Uppiano said:
Andreas Wollschlaeger said:
Steve said:
String s = (new BufferedReader(new
InputStreamReader(System.in))).readLine();
myOutputFile.println("s: " + s + "; (s == null): " + (s == null));
Output:
s: null; (s == null): false
My question is how the output is possible.
[...]
Or did you feed "null" (verbatim) as input string?
That was my guess. The stream reader read in the string "null".
That was not the case. What I was doing was testing redirecting console
input to be from a text file, using:
java [...] < simultated.console.input.txt
with WinXP.
My other guess is that this is a trick homework question that the
professor posed to his/her students.
Heh. Would have been a good one! But my problem turned out to be that I
was doing the
String s = (new BufferedReader( new ...))).readline();
to input each line of input. That works OK for a non-redirected System.in,
but with the redirection it fails for the second line of the input file.
Creating the BufferedReader just once solves the problem.
I still don't know where the literal 4-character string "null" came from --
it wasn't from the input file! And there's no such string literal in my
source code. I'm too experienced (with software, not Java) to swear that I
didn't cause the "null", but its source is still a mystery. However, having
solved my real problem (redirecting console input), I'm moving on.