aspose file tools*
The moose likes Beginning Java and the fly likes User input verification..must be integer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "User input verification..must be integer." Watch "User input verification..must be integer." New topic

User input verification..must be integer.

Vince Powers

Joined: Feb 14, 2005
Posts: 2

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.

rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
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

Joined: Dec 17, 2004
Posts: 30
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:

<a href="" target="_blank" rel="nofollow">Malware Removal</a> - Get your system running like new again.
Vince Powers

Joined: Feb 14, 2005
Posts: 2
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

Joined: Dec 06, 2001
Posts: 3061
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'.



Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: User input verification..must be integer.
Similar Threads
Assignment Java-2 (EvenOrOdd)
Command Line Argument
question about methed ?
integer command line arguments?
Check for integer input