File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

User input verification..must be integer.

 
Vince Powers
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
rahul V kumar
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
C. Alan
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic