my dog learned polymorphism
The moose likes Beginning Java and the fly likes How to read an int or double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to read an int or double?" Watch "How to read an int or double?" New topic

How to read an int or double?

Dominik Pawlocki

Joined: Oct 31, 2001
Posts: 6
Hi all ;-)
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

Joined: Dec 01, 2000
Posts: 255
To make a double from a String:

To make an int:

There's nothing more dangerous than a resourceful idiot. ---Dilbert
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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

Sun Certified Programmer for the Java� 2 Platform

Consider Paul's rocket mass heater.
subject: How to read an int or double?
It's not a secret anymore!