Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't get to next question...

 
Mark Adams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
Mark Adams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Thank you so much Kelvin, Your awsome!!!

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic