Forums Register Login
print() problems
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) {}
You will have to put the read part within a loop.
Thanks for the help, but I was still not able to get it to work.
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.
Are you using some IDE or running the program in a console?
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?
I am using JEdit 4.1
Does it open up a MS DOS Console ?
Your posted code seems to work well for me.
Are you able to compile it on your computer?
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?
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 ]
One more thought: you might want to (temporarily) replace


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.
That new kid is a freak. Show him this tiny ad:
global solutions you can do at home or in your backyard

This thread has been viewed 1098 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 23, 2019 02:29:27.