wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes check if is a number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "check if is a number" Watch "check if is a number" New topic

check if is a number

Cathy Young
Ranch Hand

Joined: Mar 02, 2002
Posts: 126
I have some strings and need to check if some of them are numbers. I am wondering if there is any existing API or implementation for this.
I thought of two ways to do it, but was not happy with either of them.
1. check the string character by character to see if it is a number. -- this is too boring and inefficient.
2. try Integer.parseInt (String) or Float.parseFloat (String) to see if NumberFormatException is thrown. I am worrying if there is any potential problem and it is too expensive to do this.
Any solutions or suggestions?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60783

(2) is the way I do it.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Cathy Young
Ranch Hand

Joined: Mar 02, 2002
Posts: 126
Thanks, Bear!
Don't get me started about those stupid light bulbs.
subject: check if is a number
Similar Threads
how to know the no of occurance in a string
How to get or replace the backslash in Java string
Using string.split with any delimiter
looking for password check class