Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   NumberFormatException (http://www.velocityreviews.com/forums/t150807-numberformatexception.html)

morc 03-02-2006 03:52 PM

NumberFormatException
 
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


Boris Stumm 03-02-2006 04:05 PM

Re: NumberFormatException
 
morc wrote:
> int x = Integer.parseInt(Double.toString(y));


Makes me think of www.thedailywtf.com. Try:
int x = (int) y;

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


Read the API doc.

James McGill 03-02-2006 04:21 PM

Re: NumberFormatException
 
On Thu, 2006-03-02 at 07:52 -0800, morc wrote:

> 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.



morc 03-02-2006 04:26 PM

Re: NumberFormatException
 
thanks it worked.

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


James Westby 03-02-2006 04:39 PM

Re: NumberFormatException
 
morc wrote:
> 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

Patricia Shanahan 03-02-2006 07:41 PM

Re: NumberFormatException
 
morc wrote:
> 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

James McGill 03-02-2006 08:08 PM

Re: NumberFormatException
 
On Thu, 2006-03-02 at 08:26 -0800, morc wrote:
> 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.


Roedy Green 03-03-2006 01:04 AM

Re: NumberFormatException
 
On 2 Mar 2006 07:52:41 -0800, "morc" <questionmarc420@msn.com> wrote,
quoted or indirectly quoted someone who said :

>public int yCoord(double y){
> y=350-(y*25);
> int x = Inte


see http://mindprod.com/applets/converter.html

It will show you the easiest and most direct way to convert from an A
to B.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green 03-03-2006 01:05 AM

Re: NumberFormatException
 
On 2 Mar 2006 08:26:47 -0800, "morc" <questionmarc420@msn.com> wrote,
quoted or indirectly quoted someone who said :

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


dump out the intermediate string. It should be instantly obvious.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green 03-03-2006 01:07 AM

Re: NumberFormatException
 
On Thu, 02 Mar 2006 09:21:23 -0700, James McGill
<jmcgill@cs.arizona.edu> wrote, quoted or indirectly quoted someone
who said :

>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;
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.


All times are GMT. The time now is 07:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.