Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Please help me!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, every one, I meet some code like the following:
import java.io.*;
public class Ppvg
{
public static void main(String argv[])
{
Ppvg p=new Ppvg();
p.go();
}
public void go()
{
try
{
int ch=0;
DataInputStream dis=new DataInputStream(System.in);
dis.read();
}catch(Exception e){}
System.out.println("continue");
}
}
when i complie and run it, it will pause until a key is hit, then
it will print "continue" and end.
why do it run like that?
and I try to creat a file "system.in" by hand in the same directory,and modify the code to the following:
import java.io.*;

public class Ppvg
{
public static void main(String argv[])
{
Ppvg p=new Ppvg();
p.go();
}
public void go()
{
try
{
int ch=0;
DataInputStream dis=new DataInputStream(System.in);
while((ch=dis.read())>-1)
{
StirngBuffer buf=new StringBuffer();
buf.append((char)ch);
System.out.println(buf.toStirng());
}

}catch(Exception e){}
System.out.println("continue");
}
}
the programe will run, but nothing to be printed
someone can tell me why, thank you very much;
if there is any wrong whit the codes , please contact me.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your name does not comply with the JavaRanch naming policy. Please spare a moment and re-register with a name that meets the requirements. We will take care of deleting your current login.
Thanks!
Ajith
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very sorry to make such trouble.
I register a new accounts again:
David chenjl
if it has any wrong, please contact me.
thank you very much.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you dind't make any trouble! Thanks for re-registering.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Why does the program wait until a key is hit and then it ends?
dis.read() blocks until a character is read from the keyboard. You wrapped System.in with the DataInputStream so the DataInputStream is looking for input from the keyboard.
2) Why does nothing print?
This gets a little tricky. As you type characters, nothing actually gets sent until the "enter" key is hit. And then all the characters get sent but are read one at a time. Even the EOL and EOF are read as separate characters.
So this code:

will print:
97|13|10|
if you type an "a" followed by "enter"
Also, the -1 doesn't get sent until the end of the stream occurs. "Enter" will not end the stream. Try CTRL+Z.
[This message has been edited by Thomas Paul (edited July 05, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic