Reputation: 6139
I have two cases
Get last value into a vector. so Vector cls contains 1. So for further processing I am converting it to integer
int c = Integer.parseInt(cls.get(0));
this works fine But for 2 nd case it fails.
Vector cls contains 1.0 and while converting to int it fails with NumberFormatException
int c= Integer.parseInt(cls.get(0));
Caused by: java.lang.NumberFormatException: For input string: "1.0" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
What can I do as a workaround by keeping Vector as String itself. Edit I need to work my code for both the cases.
Upvotes: 0
Views: 13121
Reputation: 4533
int c = 0;
try {
c = Integer.parseInt(cls.get(0));
} catch(NumberFormatException e) {
double d = Double.parseDouble(cls.get(0));
c = (int) d;
}
Upvotes: 5
Reputation: 1
you should use type float, like float c = Float.parseFloat(cls.get(0));
Upvotes: 0