• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic little program - getting user input

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just messing about trying to write little bit's and bobs, and have written this :



All the program is meant to be doing is asking the user a question and then giving an appropriate response, if the user wants to try the question again the program loops around. The scanner in the do while loop isn't actually taking any input though

Any help much appreciated
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Philips wrote:The scanner in the do while loop isn't actually taking any input though


Where are you checking the value returned from the scanner ? Immediately after the call to scan.nextLine or after you have overwritten it on line 27 ?

Also, don't use == to compare Strings.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Stuart -


Where are you checking the value returned from the scanner ? Immediately after the call to scan.nextLine or after you have overwritten it on line 27 ?



Ah I mixed up on line 27, it should be userContinue = continueAnswer;

I forgot about the .equals method as well....

I've had a play and I'm getting confused still, line 36 doesn't appear to be letting me input and I'm not sure why
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah I've changed that, I put a dummy String (line 35) in that I remember seeing somewhere (though I'm not too sure why this works exactly) So the scanner reads now... Though I fall into a bit of an infinite loop on the while that follows

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Philips wrote:Though I fall into a bit of an infinite loop on the while that follows


What in that while loop changes the value of userAnswerInt ?
Check the code very carefully before you answer that.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing that userAnswerInt = scan.nextInt(); is the incorrect answer to this as it seems like the obvious one, and It's not really changing the value if it just keeps spinning around endlessly. I thought that scanner automatically waited for user input for some reason...

I've changing the value of userAnswerInt within the while loop, but it keeps looping round whether it's the correct answer or the wrong one...

I'm not really sure
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to my question was actually : nothing in the while loop changes the value of userAnswerInt

What do braces { } do ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The more you change, particularly if you comment bits out rather than deleting them, the harder the code becomes to read. Also the longer the main method gets. You should avoid long methods.

One of your methods should read a line from a Scanner. I would actually put that in a different class. Have you been told what Scanner#nextLine() does? If yes, were you told correctly? There are even books which give the wrong answer to that question. A few hints about Scanner will be found here and here.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of those links contains a daft mistake.
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:What do braces { } do ?



Without a google they contain code.... Anything declared within braces isn't recognisable outside of them...

Oh god I've just slapped my face. Argh I forgot the braces

OK I've amended that a bit now!



I think it works? If there are any faux pas in there then it'd be great to high light them.

I think I'm going to try and generate questions some how rather than them being fixed in the code...
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The more you change, particularly if you comment bits out rather than deleting them, the harder the code becomes to read. Also the longer the main method gets. You should avoid long methods.

One of your methods should read a line from a Scanner. I would actually put that in a different class. Have you been told what Scanner#nextLine() does? If yes, were you told correctly? There are even books which give the wrong answer to that question. A few hints about Scanner will be found here and here.



Cheers Campbell - I was uncertain as to whether I should have split this up, it felt a bit stretched out...

Thanks for the link I'll have a look, I've read stuff about the scanner.... Reads the system input? Hopefully that link will iron thing's out a bit
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Philips wrote: . . . I've just slapped my face. Argh I forgot the braces . . .

That is why you should have a consistent indentation policy. You can see where the braces are so much more easily.
reply
    Bookmark Topic Watch Topic
  • New Topic