• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

strange behavior - System.in/out

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic