| Author |
strange behavior - System.in/out
|
thomas
Ranch Hand
Joined: May 26, 2002
Posts: 79
|
|
When I run the following code and type CTRL-Z (on Windows PC), then System.in.read() should return -1 and System.out.println("1st echo - " + i) and System.out.println("2nd echo - " + i) should echo it on the screen. This is the way things happen if CTRL-Z is the first thing I type after running the program. If I type CTRL-Z after typing some other input, then I do NOT get the first echo! Can somebody explain? Thanks in advance! <PRE> import java.io.*; public class test { public void run() { int i = 0; try { while (i!=-1) { i = System.in.read(); System.out.println("1st echo - " + i); System.out.println("2nd echo - " + i); } } catch(IOException ioe) { System.out.println("caught IOException"); } } public static void main(String[] args) { new test().run(); } } </PRE>
|
 |
Paul Selby
Ranch Hand
Joined: Oct 20, 2000
Posts: 41
|
|
This may not be particularly useful, but this is the result from attempts. I typed 'a' then CRTL-Z: a1st echo 2nd echo 1st echo 2nd echo This was done from a DOS prompt. Is it possible that the way you are executing is not showing the first line even though it has been outputted?
|
 |
Paul Selby
Ranch Hand
Joined: Oct 20, 2000
Posts: 41
|
|
This might not be particulary useful, but this is the result from my attempt. I typed 'a' and then CTRL-Z. a1st echo 2nd echo 1st echo 2nd echo This was done from a DOS prompt. Is it possible that, the way you are executing it, is not showing the first line even tough it is being outputted?
|
 |
thomas
Ranch Hand
Joined: May 26, 2002
Posts: 79
|
|
Thanks Paul for responding. If I type a and then CTRL-Z, I get the same lines as you i.e. all lines one would normally expect. But when I type a, then press ENTER and then type CTRL-Z, I see the following lines on the screen: 1st echo - 97 2nd echo - 97 1st echo - 13 2nd echo - 13 1st echo - 10 2nd echo - 10 2nd echo - -1 You see, I am missing the first echo for -1. But when I direct the output to a file as follows java test > file.txt then I get all the echos correctly 1st echo - 97 2nd echo - 97 1st echo - 13 2nd echo - 13 1st echo - 10 2nd echo - 10 1st echo - -1 2nd echo - -1 Could it be that the input and output are getting mixed up in some way when both show up in your DOS window?
|
 |
Mapraputa Is
Leverager of our synergies
Sheriff
Joined: Aug 26, 2000
Posts: 10065
|
|
Thomas, our official policy on registered names was changed. Please, look at this post for more details. [This message has been edited by Mapraputa Is (edited October 22, 2000).]
|
Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
|
 |
 |
|
|
subject: strange behavior - System.in/out
|
|
|