• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner Look Ahead  RSS feed

 
Robert Richard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,
I'm working on a program that uses scanner look scanner look ahead methods to show the following output:
Please enter an integer value in the interval 1 to 100 (Enter 0 to stop): what?
what? is not an integer.
Please enter an integer value in the interval 1 to 100 (Enter 0 to stop): 18.4
18.4 is not an integer.
Please enter an integer value in the interval 1 to 100 (Enter 0 to stop): 80
Please enter an integer value in the interval 1 to 100 (Enter 0 to stop): 123
123 is not in the interval 1 to 100.
Please enter an integer value in the interval 1 to 100 (Enter 0 to stop): q
q is not an integer.
Please enter an integer value in the interval 1 to 100 (Enter 0 to stop): 40
Please enter an integer value in the interval 1 to 100 (Enter 0 to stop): 0
Program stopped, check out the DrawingPanel.

I'm posting the code I wrote for the exercise before this. It's good it you are only working for integers that are either inside or out side the range. What I need it to, and I have spent the last 2 days trying to make it do is output what I posted above. The exercise wants me to use .hasNextInt() and console.next() to accomplish that. Also I should mention that we are not allowed to modify the main method. Just the getNextValue() method.


If anyone would be willing to point me in the right direction I would appreciate it.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Robert, and welcome to the Ranch!

What specific problem are you having? What output are you getting that's different than what you should get?
 
Robert Richard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably the closest I've gotten:


Output:
Please enter an integer value in the range 1 to 100 (Enter 0 to stop): you

898
99
898 is not in range 1 to 100.
you is not an integer.


However, on most other variations of getNextValue() that I have tested I have gotten an error message in return.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure about while (myScanner.hasNextInt()) ...? Compare what you have with what Rob Spoor wrote two years ago.
 
Robert Richard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you sure about while (myScanner.hasNextInt()) ...? Compare what you have with what Rob Spoor wrote two years ago.


Aha! Thank-you for pointing that out to me!
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!