• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Can't get to next question...

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Thank you so much Kelvin, Your awsome!!!

Mark
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic