Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
9  
an Integer is not a decimal type. When you have decimals, try with Double or Float instead. – Stultuske Jun 30 '15 at 10:15
1  
its a double so you have to use double d = Double.parseDouble(cls.get(0)); int i = (int) d; – Karthigeyan Jun 30 '15 at 10:17
    
How can I work for both cases? – Unmesha SreeVeni Jun 30 '15 at 10:21
    
catch and try again for double/float... – Garry Jun 30 '15 at 10:24
int c = 0;

try {

    c = Integer.parseInt(cls.get(0));

} catch(NumberFormatException e) {

    double d = Double.parseDouble(cls.get(0)); 

    c = (int) d;
}
share|improve this answer
int c=(int)Double.parseDouble(cls.get(0));
share|improve this answer

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

share|improve this answer
    
Should be a comment – shapiro yaacov Jun 30 '15 at 14:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.