Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read an int or double?

 
Dominik Pawlocki
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ;-)
Situation:
I'm writing an math application,and I need some fields in my GUI to enter the calculation params(lenght for example).
The TextField with getText method (you have explained me grately) returns a String;I need the method read a double or int from the field to use in aplication's further calculations.
How shall I do this?
And what about sending the result back to my window? (setText or drawText use Strings too)
Thank's very much.
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make a double from a String:

To make an int:

 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dominik
To get an int or double from a string you'd use one of the wrapper classes. Check out the Integer and Double classes in the java.lang.Number package. Both of these classes have methods that will take a String value and return a primitive. For example to get a double from a String you would do somehting like this:

After that code the variable dbl will have the primitive double value of 2.456
Hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic