Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Problem with simple read from standard input

Thread view: 
Jonathan - 17 Feb 2005 06:52 GMT
I havn't been using java for long, but I don't remember having a problem
with this before. I'm trying to do a simple read from the System.in, and for
some reason my BufferedReader.readLine() method returns instantly with a
null... it never actually prompts me for input. Why is this????

String cmd = "0";
System.out.print("Enter a command: ");
try {
   BufferedReader readerIn = new BufferedReader(new
InputStreamReader(System.in));
   cmd = readerIn.readLine();
} catch (IOException e) {
   System.out.println("error occured");
}
System.out.println("Command entered was " + cmd);

//the above program prints "Enter a command: Command entered was null"

thanks
jonathan
Jonathan - 17 Feb 2005 07:50 GMT
Just a followup. I am thinking that perhaps this problem is somehow caused
by my IDE, NetBeans? I'd still appreciate help/comments from anyone.

> I havn't been using java for long, but I don't remember having a problem
> with this before. I'm trying to do a simple read from the System.in, and for
[quoted text clipped - 16 lines]
> thanks
> jonathan
Roland - 17 Feb 2005 10:47 GMT
> Just a followup. I am thinking that perhaps this problem is somehow caused
> by my IDE, NetBeans? I'd still appreciate help/comments from anyone.
[quoted text clipped - 22 lines]
>>thanks
>>jonathan

Netbeans 4 perhaps?

NB4.0 appears to have a problem with reading input from System.in when
the application is run from within NB. I'm not up to speed with Netbeans
(normally use Eclipse), but the help on "Output Window" mentions the
following:
<quote>
When you run a program that requires user input, a new tab appears in
the Output window. This tab includes a cursor. You can enter information
in the Output window as you would on a command line.
</quote>
This input tab, however, does not appear when I tried a program that
reads from System.in. Probably with the new build system of NB4 (using
Ant), input to commandline programs has become difficult, or maybe
impossible.

Someone with experience in NB4 maybe knows how to enter keyboard input
for a command line program. Otherwise, if Netbeans has support forums
(undoubtly they have), you might want to ask there.
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Jonathan Halterman - 17 Feb 2005 19:42 GMT
> > Just a followup. I am thinking that perhaps this problem is somehow caused
> > by my IDE, NetBeans? I'd still appreciate help/comments from anyone.
[quoted text clipped - 42 lines]
> for a command line program. Otherwise, if Netbeans has support forums
> (undoubtly they have), you might want to ask there.

Yes, it appears I wasn't going crazy and that this was indeed a bug in
NetBeans 4 apparently caused by Ant. It's disappointing though to see that
it doesn't seem to be taken very seriously.

jonathan
Oscar kind - 17 Feb 2005 17:09 GMT
> I havn't been using java for long, but I don't remember having a problem
> with this before. I'm trying to do a simple read from the System.in, and for
> some reason my BufferedReader.readLine() method returns instantly with a
> null... it never actually prompts me for input. Why is this????

See the API docs for java.io.BufferedReader#readLine(), regarding the
return value:
    A String containing the contents of the line, not including any
    line-termination characters, or null if the end of the stream has been
    reached

It appears System.in has reached its end, which probably means it's
closed. Did you start the program that contains your code snippet from the
command line?

Also, I suggest you either bookmark the following link, or download the API
docs from Sun. It describes all standard classes, and what they do.

    http://java.sun.com/j2se/1.5.0/docs/api/index.html

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.