Integer.parseInt() has to take a string that is an integer (i.e. does not have decimal points, even if the number is equivalent to an integer. The exception you get there essentially says βyouβ, ve told me that this number is an integer, but this string is not in a valid format for an integer! "
If the number contains a decimal component, you need to use Double.parseDouble() , which will return a double primitive. However, since you are not interested in the decimal component, you can safely abandon it by simply translating double into int:
int num = (int)Double.parseDouble(str);
Note, however, that this will simply lower the decimal component; it will not round the number. So casting 1.2, 1.8, or 1.999999 to int will give you 1. If you want to round the number that is returned, use Math.round () instead of just clicking on int.
berry120
source share