aspose file tools*
The moose likes Beginning Java and the fly likes print() problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "print() problems" Watch "print() problems" New topic
Author

print() problems

Al Rivarosa
Greenhorn

Joined: Sep 12, 2003
Posts: 5
I am just now learning to program in Java, and I am having a problem with getting user input after the print() statement runs. Instead of the cursor waiting for user input, it just runs through the rest of the code.
public class Shift {
/**
* The main program for the Shift class
*
*@param args The command line arguments
*/
public static void main(String[] args) {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String value ");
String number = null;
try {

number = console.readLine();
} catch (IOException e) {}
System.out.println(number);
}
}
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

You will have to put the read part within a loop.


Groovy
Al Rivarosa
Greenhorn

Joined: Sep 12, 2003
Posts: 5
Thanks for the help, but I was still not able to get it to work.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Not sure what you mean by "it just runs through the rest of the code." The program you've written will work just as you'd expect, with two possible exceptions, both somewhat platform-dependent.
The first issue is that the prompt might not appear before you type the input. You can fix this by calling System.out.flush() after printing an incomplete line, like this:

The second issue is with line input buffering. Some operating systems will line-buffer the console input to this program; the problem arises because that buffering will interact with the buffering done by the BufferedReader class. Whenever you connect a BufferedReader to a line-buffered input source like System.in, you should add the optional second argument to the BufferedReader constructor, specifying a buffer size of 1 character (i.e., no buffering

With thrse changes your program should run as expected on any platform.


[Jess in Action][AskingGoodQuestions]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Are you using some IDE or running the program in a console?
Al Rivarosa
Greenhorn

Joined: Sep 12, 2003
Posts: 5
It still does not work, on my machine. But it runs fine on my friends computer. Could my PATH or CLASSPATH not be set correctly?
Al Rivarosa
Greenhorn

Joined: Sep 12, 2003
Posts: 5
I am using JEdit 4.1
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Does it open up a MS DOS Console ?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Al,
Your posted code seems to work well for me.
Are you able to compile it on your computer?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
I have heard of this problem before. The frustrating part is that your code works fine for me so I can't duplicate the problem. As to the cause of your problem, it seems that in some environments, a residual CR/LF remains in the console input buffer when the program is started. This residual CR/LF is being read when the readLine() method executes. Thus, your program appears to just "run through" the call to readLine(). To see if this is the cause of your trouble, try adding a second readLine() statement immediately after the first. The first readLine() will catch the residual CR/LF, the second will read your intput. If this works, then you will at least know what is happening.
BTW, what operating system are you using?


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ahem.
Funny, I just got done ranting about this sort of thing elsewhere.

Empty catch blocks are EEEVIL, EEEVIL, EEEVIL! Unless you're sure of what you're doing; there are some exceptions which we won't go into here. Any time you're not 100% sure what how to deal with an exception, do not ignore it. Replace this with:

Chances are good you will now see an error message here, giving a clue what the problem is. You never want to hide such an error message. Later on, for more user-friedly programs, you might log the error message to a file somewhere instead, and pop up a nicer looking message to the user instead. But e.printStackTrace() is a good quick default for now.
[ October 02, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
One more thought: you might want to (temporarily) replace

with

This is useful for seeing exactly what sort of string you did read, if the problem has to do with carriage returns and line feeds as Herb suggests. If some of the chars you've taken in are not printable, this helps you see what they really are.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print() problems