Java – Getting Error java.lang.NumberFormatException: For input string: “16.0”

java

I want to convert the cell value into int 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 accepting 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 or vice versa?

Best Solution

The problem is the ".0" bit. Integer.parseInt only allows digits so the decimal point is illegal, hence the NumberFormatException.

You should make sure that your input really is an integer (i.e. "16"), or if you actually want to allow decimals then use Double.parseDouble or Float.parseFloat.