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 number from String 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 "number from String" Watch "number from String" New topic

number from String

Joe Wolf
Ranch Hand

Joined: Jul 05, 2001
Posts: 42
Given a String, is there a way to make sure that it's a valid real number, containing only the digits 0 - 9 and has no more than one decimal point? Would attempting a cast and catching an exception work or is there a better way?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Check the API for parseInt(). There might be a parseFloat() or parseDouble()

So much trouble in the world -- Bob Marley
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
You could use valueOf(), pass the string to it. If it ain't a number that will fit within the wrapper object you're using, it will throw a NumberFormatException.
If you wanted, you could use a bunch of if statements or something like that, but I would imagine the best way would be using a try/catch( NumberFormatException e ) block
[This message has been edited by jason adam (edited August 16, 2001).]
I agree. Here's the link:
subject: number from String
It's not a secret anymore!