solving java.lang.NumberFormatException ...

J

June Moore

Hi,

I'm trying to convert String to double as follows.

String s = "100";
double d = new Integer(s).doubleValue();
--> Works OK.

String s = "100.55";
double d = new Integer(s).doubleValue();
--> I got java.lang.NumberFormatException.

How do I solve this problem?

Thanks,
June.
 
K

KD

Hi

You are approaching this the wrong way. Do as follows:

String i = "100";
String d = "100.55";

int intprim = Integer.parseInt(i);
Integer intObj = new Integer(i);

double doubleprim = Double.parseDouble(d);
Double doubleObj = new Double(d);

NumberFormatException is occuring because you cannot convert a real
number to an integer.

-Karim
 
T

Thomas Weidenfeller

June said:
String s = "100.55";
double d = new Integer(s).doubleValue();
--> I got java.lang.NumberFormatException.

How do I solve this problem?

Gee, by not using the Integer class when you want to deal with doubles.
Use the Double class.

/Thomas
 
A

Arild Skaar

Hi,

I'm trying to convert String to double as follows.

String s = "100";
double d = new Integer(s).doubleValue();
--> Works OK.

String s = "100.55";
double d = new Integer(s).doubleValue();
--> I got java.lang.NumberFormatException.

How do I solve this problem?

Thanks,
June.

Unless you want to round the number, you shouldn't use Integer in this
scenario.
This should work better:

String s = "100.55";
double d = new Double(s).doubleValue();

Regards, Arild
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top