wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Can't get to next question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't get to next question...

Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
Please help, I am unable to get this code to prompt for next question and so on... What am I doing wrong?

Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Mark,

After you enter "m" and hit the ENTER key, the input stream will contain not just the 'm' character, but also the character(s) corresponding to the ENTER key. Depending on your operating system, this will usually mean that you have an extra '\n' character, or an extra '\r' character, or even an extra "\r\n" sequence of two characters.

As a result, your second read() call will read this extra character off the input stream and return immediately, instead of pausing for user input. One way of fixing this would be to modify your second read() call to look like this:


(P.S.: You may also want to re-consider your placement of your if statements. What happens if the user types 's' or 'p' in response to your first prompt?)


SCJP 5.0
Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
Ok, Thank you.

I did what you suggested and it is finally getting there however now it just stops, do I have to put what you suggested in every time I need to prompt for a response?

In resopnse to your P.S., I realize I have much more error checking to do but I am just trying to get my my main questioning part going first.

Mark
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Mark Adams:
I did what you suggested and it is finally getting there however now it just stops, do I have to put what you suggested in every time I need to prompt for a response?


Yes. To avoid excessive code duplication, you will probably want to wrap this up in a separate utility method, e.g.

which will then allow you to simply get the user's responses using just:


(Note that a more general solution would probably wrap System.in within a InputStreamReader and possibly also a BufferedReader. But the above is likely to suffice for your needs.)
Mark Adams
Greenhorn

Joined: Nov 23, 2007
Posts: 16
Ok, Thank you so much Kelvin, Your awsome!!!

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get to next question...
 
Similar Threads
ARRAYS AND STRINGS
Simple input commands
Illegal start of expression
ARRAYS AND STRINGS
Break back to Topic Menu