• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Wanting input to be String value only

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
If you want user input to be strings only and you want to catch  any integers entered by the user, how do you do it?I want the user to enter string values only and to catch any integers entered, when I input integers its doesn't display the error message.





Thanks
 
Marshal
Posts: 80874
506
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The input to the command line/standard input stream is always in text format. Everything you pass into it is a String. You cannot get a number from the standard input stream, you can only take the input and parse it as a number. Which you are not doing. Any text counts as a valid String, even the empty (0‑length) String. There is no built‑in way to test that text constitutes a word in English. You can try testing the input to see whether it is a number. Pass the text to a Scanner constructor and try its hasNextXXX() methods. Or you can try a regular expression to test for letters. Neither approach is really good however.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this. But remember return string value can contain a numbers i.e. "1an3z".


The second way is to use static method paseInt() which throw exception if the string does not contain a parsable integer.

 
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple of questions that might help. Why do you not want any numbers? If you are trying to validate data to make sure its a country "asdfjkansdfjansdfajdfna" is just as bad as "Ireland99".

Second, I feel this might be a really good situation to use an enum for country names as the countries you are looking for are static. Just a thought.

-Zach
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Grzegorz Kędzior wrote:. . . static method paseInt() which throw exception if the string does not contain a parsable integer.

Please consider whether that really is a good use for exceptions.

You misspelt the method name: it is Integer#parseInt(...)
reply
    Bookmark Topic Watch Topic
  • New Topic