Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using Double

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

Thanks.
 
Don Smathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic