• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Wanting input to be String value only

 
Ranch Hand
Posts: 110
  • 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: 74044
332
  • 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: 74044
332
  • 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(...)
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic