It's not a secret anymore!
The moose likes Java in General and the fly likes using Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "using Double" Watch "using Double" New topic

using Double

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I am getting some numeric text from a JTextField using the JTextField.getText() method. When I use the String returned to instantiate a Double I get a NumberFormatException. Can anyone tell me what I'm doing wrong? If I replace the String variable name with a "99.9" for example, I don't get a problem.
My code looks roughly like this :
JTextField jt = new JTextField("66.8");
String number = jt.getText();
Double d = new Double(number);

Don Smathers
Ranch Hand

Joined: Mar 04, 2001
Posts: 31
I haven't tried this, but it might work:
double nbr = Double.parseDouble( number ) ;
which should convert "123.456" to 123.456
but, be careful -- if your string is " 123.3" or "cde123.4" the class won't handle the complexity.
Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Originally posted by Don Smathers:
double nbr = Double.parseDouble( number ) ;

If you are using JDK1.1.x you have to use
since there is no Double.parseDouble(String)
I agree. Here's the link:
subject: using Double
It's not a secret anymore!