File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion 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: 63873

(2) is the way I do it.

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

Joined: Mar 02, 2002
Posts: 126
Thanks, Bear!
I agree. Here's the link:
subject: check if is a number
jQuery in Action, 3rd edition