Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Convertinf string to double from JTextField

 
Alex Perez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!