views:

417

answers:

2

Is there a way to convert the string from a text field to a double in j2me? And what is the name of the string that comes out of the text field?

+1  A: 

You can use Double.parseDouble(String) method for converting string to double:

double d = Double.parseDouble("22.4"); 

To get text of TextField you can use TextField.getString() method;

String text = TextField.getString();

So:

double d = Double.parseDouble(TextField.getString()); 
JCasso
`valueOf()` returns a Double object. In Java 5+ that can be auto-unboxed to a `double`. In J2ME this autoboxing doesn't work, so your second line of code won't compile (even in Java 5+ it does unnecessary work and `parseDouble()` should be prefered, when a `double` is needed).
Joachim Sauer
Right. Thank you.
JCasso
A: 

Make a note that floats are only supported on phones that conform to CLDC 1.1, if the phone you are targetting is CLDC 1.0 you will need to use fixed point

kgutteridge
Luckily enough CLDC 1.0 is ancient and mostly gone. CLDC 1.1 isn't the most powerful either, but at least it's got the basics.
Joachim Sauer
true, about 9% of the market according to getjarhttp://stats.getjar.com/statistics/world/gJavaCLDCVer/j2me_cldc_1_0just worth being aware of!
kgutteridge