• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
n jay
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hye.

In this particular code..im getting this error.

The root cause of it.........


The Code.......


Hope can guide me on this and can i know what is problem with above code..

thank you.

-NJ-
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Debug your servlet to see is all the values you are passing to Float.parseFloat are of the String containing a valid float number.

May be you can use sysout if you don't know how to debug.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you don't need to write "java.lang.Float" - just "Float" is sufficient, since the java.lang package gets imported automatically.
 
n jay
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hye.

Can you explain step by step...if it is possible for you.

thanks for the helpful reply.

this is the stack of it.





 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are couple of ways to check where are you making the mistake:

1. put every parseFloat inside the try catch block with catch is catching the NumberFormatException
2. put appropriate e.g. System.out.println("float cl1: " + cl1) before every parse so you know which value is not of the type float
3. third and best way is to use IDE's debug framework to diagnose at each step what values are coming this is one of the best practices of coding (if you don't know how to debug search how to debug on your IDE)

If you are not using any IDE i'll recommend you the second solution.
 
n jay
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Subodh:Hye,

thanks for the reply.

i am currently using netbean 6.8 IDE. i have done the debugging and in the debug console it gives me this,


and this is code that throw that exception.

and data was in this format:
IndexCode;Change;Currency;PrevCloseDate;SourceInterval;Tag;MSN;MessageType;LogTimestamp;

FBMAPU;-43.75;USD;20090407;15;0;0;RSP;04/08/2009 10:04:13:510

i think it not detecting the negative there..is it because of the locale?


ulf:hye

Also note that you don't need to write "java.lang.Float" - just "Float" is sufficient, since the java.lang package gets imported automatically.


Will write it again.Thanks.

Thanks
-Nj-


 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You should start the tomcat in the debug mode (should consult the appropriate forum for how to debug using Ntebeans and Tomcat).

IndexCode;Change;Currency;PrevCloseDate;SourceInterval;Tag;MSN;MessageType;LogTimestamp;

FBMAPU;-43.75;USD;20090407;15;0;0;RSP;04/08/2009 10:04:13:510

i think it not detecting the negative there..is it because of the locale?


How ever i am not able to understand who is it there? I guess you must be getting the data via splinting the input via token ";".

and if you are thinking parseFloat is unable to convert the string -43.75 to float, than this assumption is wrong it does it quite well.

is it because of the locale?


What you mean by this?
 
n jay
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hye.

How ever i am not able to understand who is it there?


can't really get what you mean by this.


is it because of the locale?


i was reading some forum and there some say that we may get that exception because of the locale setting.i was just asking...

i will try to debug it.
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i think it not detecting the negative there..is it because of the locale?


Who is it in above underlined text?

Can you tell me how are you dividing the input data?

I don't think any think to do with locale...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with the negative value. The error message already tells you which value trips it up: "Change". Now you need to investigate your program logic why that is passed into a number parsing method.
 
n jay
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hye...
i am totally don't get what you mean...
Who is it in above underlined text?

which underline word???
you must be mad but i am really sorry...
Can you tell me how are you dividing the input data?

means???
by the way i am reading from csv file and one of my friend say that this code gives me error because there are header column in csv file which is a string and that why it gives me this exception because it is trying to change the header string to float.

hye ulf,

thats what my friend say. Because the particular "change" is the header in the csv that i try to read using that code.

so i did remove the header from the csv file and run the code... i don't get exception and i don't see the table that i want.only thing appearing is the heading in the jsp page.

thanks
-nj-


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be other problems in your code, but since we don't know what all those variables are (v1[9], c4, c11 - you might want to get in the habit of using variable names that tell the reader of the code what they mean), it's not possible to speculate what's going on.

I'd advise to use one of the various available CSV libraries for reading such a file; see the http://faq.javaranch.com/java/AccessingFileFormats page for links to some of them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!