Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick check if a String argument is actually an int?

Status
Not open for further replies.

paulbradley

Programmer
Oct 9, 2002
158
GB

I have code that does certain stuff if it gets an intger argument, and other stuff if it is String.

How can I quickly check if one of my args from command line is actually an integer without doing a parseInt and catching an exception if it is not?#

Thanks
 
One way would be to convert the String to a char array, and then do Character.isDigit() on each char.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
No need to convert it to a char array, you can just loop until the lenght of the String using charAt, but that's exactly what parseInt does, and it also cares about negative signs, hex formats so ... why would you want to code something that is already done and fully tested?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top