Search code examples
javaandroidintegernumberformatexceptionnumberpicker

Android: java.lang.NumberFormatException: Invalid int: "27.0"


I haven't found an answer to this question where the decimal is 0, so...

//Value of row.get(2) = "27.0"

private NumberPicker numEntry;
numEntry.setMaxValue(40);
numEntry.setMinValue(15);
numEntry.setValue(Integer.valueOf(row.get(2)));

Error message:

05-27 14:30:51.576  10143-10143/com.exampleapp.app E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NumberFormatException: Invalid int: "27.0"

I've tried a few different methods to convert the string "27.0" into an integer 27, Math.floor, parseInt... the ArrayList row is pulled from the web via html so it starts life as a string, fyi.

How do I set the NumberPicker to this initial value?


Solution

  • I would just do

    (int)Double.parseDouble(row.get(2));
    

    so it will be

    numEntry.setValue((int)Double.parseDouble(row.get(2)));
    

    it will handle double, integers and so on. Will throw NumberFormatException only if it's a non valid number.

    You should only worry about the fact that a double could not fit in a int. But i don't think it's a problem here.