Getting Error java.lang.NumberFormatException: For input string: "16.0"

Hi,
I want to convert the cell value into integer value so i am trying following code.

for(int chk1=1;chk1<m;chk1++)

{

int intCounter = Integer.parseInt(cells.checkCell(chk1,0).getValue().toString());

}

But it is acceptinng only the string format if there is any number then it is giving me java.lang.NumberFormatException

How can i avoid this. is there any way to convert all data into integer or into String

Hi,

Your code is working fine. Just make sure, when you convert some string to number, then there is no space inside it.

For example, “12” will be converted to number, but "12 " will throw exception. Similarly, non number string will also raise exception.

Also, you can use Double, when you convert number with decimal point. e.g


double doubleCounter = Double.parseDouble(“16.0”);

System.out.print(doubleCounter);

Please note, double works for all type of numbers, decimal numbers and numeric strings.

Hi,


I think your code segment is incorrect. The values might not be integer values, and even for integer values, it may be saved as double values for Excel files. So, the cell.getValue().toString() can give a string like “16.0” which cannot be parsed as integer. We think you should implement your own logic to parse the value to integer, such as, remove decimal part from the string. If you still find the issue, kindly give us your complete sample code and (template file if you have), we will check it soon.

Thank you.

Thanks,

Is there any way to convert all data into string or integer. In my code i am checking Sequence of the sr no.

Here is my code:

for(int chk1=1;chk1<m;chk1++)
{ arrrowindex[chk1]=arrrowindex[chk1]+1;
if (cells.checkCell(arrrowindex[chk1],0).getValue()==null)
{Cell cell=cells.getCell(arrrowindex[chk1],0);
Style style = cells.getCell( arrrowindex[chk1],0).getStyle();
style.setColor(Color.TEAL);
cell.setStyle(style);
}else{
int intCounter = Integer.ParseInt(cells.checkCell(arrrowindex[chk1],0).getValue().toString());
System.out.println(cells.checkCell(arrrowindex[chk1],0).getValue().toString());
if(intCounter!=OCount)
{ Cell cell=cells.getCell(arrrowindex[chk1],0);
Style style = cells.getCell( arrrowindex[chk1],0).getStyle();
style.setColor(Color.YELLOW);
cell.setStyle(style);
}
}
OCount=OCount+1;
}



This Code woks fine if my data is in String format but it is Number then it throws NumberFormatException. So is there any other method to convert data into one format either string or int.

Hi,

I will recommend you to convert double to integer by simple type casting e.g

Java


double d = 12.65;

int i= (int)d;


System.out.println(i);

Output
12