USB
USB

Reputation: 6139

java.lang.Exception: java.lang.NumberFormatException: For input string: "1.0"

I have two cases

  1. Input-> 3,4,1

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.

  1. 3.0,4.0,1.0

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

Answers (3)

user3459464
user3459464

Reputation: 224

int c=(int)Double.parseDouble(cls.get(0));

Upvotes: 8

Garry
Garry

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

fongg
fongg

Reputation: 1

you should use type float, like float c = Float.parseFloat(cls.get(0));

Upvotes: 0

Related Questions