This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Input from keyboard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Input from keyboard" Watch "Input from keyboard" New topic

Input from keyboard

Will Carpenter

Joined: Mar 17, 2004
Posts: 26
Boy, this is probably the simplest question about Java you've seen in weeks. I just can't remember the answer.
I prompt the user for a number, the number, and later prompt for other numbers that I put into an array, but "stuff" is still left in the "stream" that "corrupts" my array.
For instance, in the following, I first choose 2, then hit enter. What is supposed to happen is I'm supposed to get prompted for the X-coord of the first point and I'm supposed to put in a number then hit enter, then I'm supposed to get prompted for the Y-coord of the first point and I'm supposed to put in a number and then hit enter....
But what happens instead is after I choose 2 and hit enter, I SEE the prompt for the X-coord of the first point, but the program doesn't wait for me to enter anything; then I SEE the prompt for the Y-coord of the first point, but the program doesn't wait for me to enter anything; then I see the prompt for the X-coord of the second point. At which point, the program waits until I enter the X-coord of the 2nd point. Say I put in 5 then enter, then the program prompts for the Y-coord, but doesn't wait for me to enter anything, it just continues on.
It then displays my array 8 0 13 10 53 13.....
You can cut-and-paste the following and run it to see what I mean. Choose Task#2.
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 17, 2004: Message edited by: Dirk Schreckmann ]
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
My understanding is this. is an InputStream. The read() method of InputStream reads a single byte. On ASCII (or similar) keyboards/terminals, this has the effect of reading one character entered and returning that character's ASCII code.
Anyway, the bottom line is that reading a char (like in your code when you select a menu item) happens to work OK, because the ASCII code of the char entered maps back to whatever char you wanted. However, reading anything else will result in undesired behaviour.
One way to go about reading a Short is:

Analogous methods in other wrapper classes (Integer etc) also exist.
Bear in mind that this approach will only work if you are expecting exactly one number per line. If you want to read more than one Short (etc) per line, look at the StringTokenizer class.
Hope this helps, I'm sure someone will correct me if I've got anything wrong

Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
An addendum -
When I was playing around with this code I forgot to add the obligatory try/catch block. In the code below I also moved the declaration of i outsides the try{} block or it'll go out of scope pretty quick

Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
I am having a problem with my program not pausing to recieve the input. I come from C++ and am looking for a way to have the program wait before moving on to my other processes. I am very perplexed as to why this is happening, but it seems like the whole buffered input is being skipped?

Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
forgot this is an example I made...

This thing is not working..

Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
It works perfectly for me. I type some stuff in and press enter and get the message as expected. Are you doing something odd in an IDE or something?

A few tips for you as you're new to Java:
  • You never need to import java.lang - it's implicit
  • Sort out your indentation; you have enough to worry about learning the language without having to battle with that too
  • Get into the habit of using Java coding style conventions. You're not far off but class names are capitalised (Printer) and people typically start the { on the same line as the method name, class name or whatever (though this varies with taste)
  • Be very wary about swallowing Exceptions as you have in your readLine() method. If something goes bang here you probably want to know rather than continuing on blindly
  • Note that closing the BufferedReader closes the underlying InputStream, in this case That's probably not something you want to do in a normal application.
  • I wouldn't use Hungarian notation either, but some people prefer it. Sun's style guidelines advise against it.
  • Import individual classes explicitly (i.e. don't use *) unless coding noddy examples like this one
  • And one last JavaRanch-related one: when you have a new question (like this) start a new thread. If for no other reason than you're more likely to get a response.

  • I've modified the style of the code below but it worked as it was:

    Dunno about your problem but I hope you find this useful.

    I agree. Here's the link:
    subject: Input from keyboard
    It's not a secret anymore!