This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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 != (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.
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.
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="http://www.security-forums.com/forum/viewforum.php?f=48" target="_blank" rel="nofollow">Malware Removal</a> - Get your system running like new again.
Joined: Feb 14, 2005
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?
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'.