NumberFormatException

M

morc

hi,

i keep gettin a number format exception :

java.lang.NumberFormatException: For input string: "245.0"

This is where the error is :

int iHigh = yCoord(4.2);

public int yCoord(double y){
y=350-(y*25);
int x = Integer.parseInt(Double.toString(y));
return x;
}

I can't figure out why this won't work. Can somebody please point me in
the right direction. It would be greatly appreciated.

Thanks alot
-morc
 
J

James McGill

I can't figure out why this won't work. Can somebody please point me in
the right direction. It would be greatly appreciated.

int x = new Integer(new Double(y).intValue()).intValue();

You can do it without creating the intermediate objects, but this is no
worse than your example of creating and parsing the number from a
string.
 
J

James Westby

morc said:
thanks it worked.

but how come parsing it the way i did doesn't work??
Because 345.0 isn't an integer, it's a double where the fractional part
happens to be zero.


James
 
P

Patricia Shanahan

morc said:
thanks it worked.

but how come parsing it the way i did doesn't work??

Note that the javadoc for Integer.parseInt(String) only gives a summary.
It defines its operation in terms of the parseInt(java.lang.String, int)
method.

Follow that link, or scroll up a bit, and you will find the full
information, including the list of conditions that cause a
NumberFormatException.

Patricia
 
J

James McGill

thanks it worked.

but how come parsing it the way i did doesn't work??

Because a period is an illegal character in a string that is supposed to
represent an integer in base 10, where only the characters "0-9" and "-"
are valid.
 
R

Roedy Green

int x = new Integer(new Double(y).intValue()).intValue();

Are you just trying to convert a double to an int with truncation?

Come now. it can be done in one operation, a converting cast.

int x = (int) y;
 
J

James McGill

Are you just trying to convert a double to an int with truncation?

Come now. it can be done in one operation, a converting cast.

int x = (int) y;

I know that, but the OP seemed to be looking for help with wrapper
types in particular.
 
J

JPractitioner

Double.toString(y) should be used to change y from double to become
String object.
When u do Integer.parseInt(String ...) there, the String value expected
is from the int type. but since you have the String which originated
from a double, The Integer.parseInt(String ..) will try to do
Integer.parseInt(x.0). Please note that the .0 there is why you get the
exception.

u try do do double d = 10 and you will get error as well.

whenever u want to change something bigger (i.e double) to be something
smaller (i.e int), you use casting. use all these parses when you need
to do calculation with String value. Avoid unnecessary complexities.

thanks.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top