• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to check if the user input is a string of an int

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to get an int input from the user. I want to validate the input but I don't know how to check if the input is a word, like "Hi" (in which case the program will crash), or a number (something I want for user input). Is there a method int he API that checks this? Below is my code if you'd care to see it. If you see, the conditional statement for if is blank. This is where I check if it's a string or a number. Any ideas how i can accomplish this??

 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just useand then see if Integer.parseInt(value) is successful
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use keys.hasNextInt().
 
Saloon Keeper
Posts: 14498
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although you may want to lead with hasNext() to determine if there was user-input at all.
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you use hasNextInt() as Paweł said. No need for next or hasNext.
 
Stephan van Hulst
Saloon Keeper
Posts: 14498
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad, I thought hasNextInt() returned immediately in the absence of user input. I'm not sure why. Probably a lack of coffee.
 
Campbell Ritchie
Marshal
Posts: 76854
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. If you don't give hasNextInt enough coffee, it will sulk and sit there waiting for input. The documentation says that

The API documentation wrote:The scanner does not advance past any input.

… and in the introduction of the same page it says

A scanning operation may block waiting for input.

 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic