Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

hasNextInt ,why is my else looping?

 
Ranch Hand
Posts: 262
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my boolean hasNextInt is obviously false if i enter anything but a digit, and thats just looping round and round
how can i get my else working that it asks to enter a digit again?


 
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You call hasNextInt() but you never call nextInt() to get the value out of the buffer.

A better loop might be
 
wayne brandon
Ranch Hand
Posts: 262
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i might have asked this some time back, which is the preferred way to verify data? using try catch or in while loops using hasnext etc?
does it not really matter, depending on what you doing?
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By now you should be using your "Keyboard" (or whatever you named it) utility class. As you found out in the prior discussion, some of us prefer try/catch and some prefer hasNextXXX(). See prior thread for some of the reasons.
 
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that hasNext() tests the input you are looking at with a regular expression (=regex). Don't bother writing your own, because that regex has been tested millions of times. The Scanner documentation tells you what they are looking for so you can work out what they are using as a regex anyway. Only in the case of dubious results (e.g.& a match for the int regex but ten digits) would Scanner use try‑catch. In which case, I think you will get better execution with a loop than catching an exception.
 
wayne brandon
Ranch Hand
Posts: 262
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi carey

"By now you should be using your "Keyboard" (or whatever you named it) utility class."

i just dont find it practical when doing basic exercise drills on stuff, am i wrong?

 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:"By now you should be using your "Keyboard" (or whatever you named it) utility class."
i just dont find it practical when doing basic exercise drills on stuff, am i wrong?


I'm not sure what you mean by "practical". Utilities are very practical in my mind. They are self contained and presumably debugged from using them in several applications.

If you mean minimalist (smallest amount of code to achieve an end result), Then extracting just the constants and methods you need into your app is another way to go. What you do not want to do is reinvent the wheel. If you have code in your utility that is thoroughly debugged then use it rather than starting over.

So, if your utility has promptInt() and you might have called that except for your desire to limit your code base to only things that are directly or indirectly used, then copy the code for promptInt() into your project and call that one instead. Of course if there's any constants in your utility you may need to duplicate them as well. A side benefit is that your app will have a concise method to call, thereby promoting modularity. And, should you desire later to use the utility it would be a piece of cake to swap it in and remove the redundant methods. Alternatively, you could make a copy of your utility and hack away anything you are not directly using.
 
wayne brandon
Ranch Hand
Posts: 262
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just meant having to attach the file etc for a small exercise than just creating a scanner object, but i see your point and will follow your advice, thanks Carey
 
Carey Brown
Saloon Keeper
Posts: 6040
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have more than one class in a single .java file but only one of them can be public. This is very useful for small programs.
 
Campbell Ritchie
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . This is very useful for small programs. . . .

You can even run such a small program without using the javac tool.. That works in Java12+ only.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!