• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

User input verification..must be integer.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a rather simple delema. How do I check if the command line argument is of integer type without using try catch? I would like to test this with an if statement. The command line arguements are stored in an array.

Is it somthing like this:

if args[1] != (this is where I'm stuck. Hoe do I specify check for number or integer value. This should handle wrong input like if the user entered a char etc.

TKS
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a function isNumberand let it return a boolean for you.

then from your if statement you could call

if (isNumber(String)) {}

Now its up to you as to how you want to implement this isNumber() function.
You can check character by character in the string passed for 0 to 9 or you could have a try catch block and have Integer.parseInt(String) in it.

Hope this helps.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without knowing exactly what you want to do if there isn't an int in there,or how it is getting the input, you could do something like this:

That bit of codewill not even allow a char other than a digit to be entered. I borrowed it from Sun's website. If you want it to display a message, you could change the code to maybe something like this:

 
Vince Powers
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the user enters a char instead of a required number as a command line argument, can this be handled by a simple if statement?

how do I compare a character to an integer?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the Scanner class will be helpful. I'm not sure what it does if it cannot parse an integer when requested to, but you can check the Javadocs yourself.

Another option to do by hand is to check that each character in the string is a digit. You can't compare characters to integers directly. However, you can compare if the character represents an integer, say something like '1'.

HTH

Layne
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic