aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "number from String" Watch "number from String" New topic
Author

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()
------------------
Bosun


Bosun (SCJP, SCWCD)
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: number from String