File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Convertinf string to double from JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convertinf string to double from JTextField" Watch "Convertinf string to double from JTextField" New topic
Author

Convertinf string to double from JTextField

Alex Perez
Greenhorn

Joined: Sep 18, 2003
Posts: 1
I want to convert a string from a JTextField gotten using field.getText();
to a type double. I tried using valueOf, but then I can't use the + operator or any other math operand. I am making a calculator program in Swing.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
How about:
double dbl = (new Double(field.getText())).doubleValue();
You'll probably want to wrap it in a try/catch block in case the user puts in something invalid. You could also use regular expressions to do some validation and warn the user if it's bad.
 
wood burning stoves
 
subject: Convertinf string to double from JTextField
 
Similar Threads
Text to Bitmap convertion
Using JTextfields in a JTabbedPane
How to verify input in the JTextField?
using Double
Able to Copy Text from Text Boxes(JTextfields) in Linux/Solaris