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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking number string

Status
Not open for further replies.

lowbk

Technical User
Nov 26, 2001
162
SG
hi, i need some advice on checking number string.
eg input is "1234" and i need to verify if it is numeric.

currently i go through each character within to see if it is a digit; if all are digits then it will return true. but this looks very long-winded.

can anyone advise on a much neater way?
thanks
 
try using 'Integer.parseInt(String num)' within try block and catching the NumberFormatException

Hope it helps

regards
raochetan
 
You can try to match it with a regular expression.
Code:
someString.matches ("\d+")
will return true for one or more digits and false for anything that is not completelty numeric. If you are using floating point, try
Code:
someString.matches ("\d*.?+\d*")
. It allows for the decimal char and numbers in front and behind.

Hope this helps,
MarsChelios
 
marschelios, i looked through the java api but there is no method "matches". is this method from the api or is it a self-defined one?
 
Hi all,
I found another error with the Regular Expression for floating-point numbers. The . character is a special character which allows any character to replace it. To use the actual ., prefix it with \.
The Regular Expression:

("\d*.+\d*")

should be changed to:

("\d*\.+\d*")

Hopefully, this is the last error I find with this. [smile]
MarsChelios
 
Hello all,
My FAQ on Regular Expressions has been posted, Let me know what you guys think.

MarsChelios
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top