Convert String to Long using BufferReader

69 views Asked by At

I'm trying to convert my input read Strings to long (Long). I've tried the valueof() and Long.Parslong(String s) but no luck. Im not sure what is going on. I can certainly print the String but cannot convert to Long ):

while ((line = br.readLine()) != null) {
                String[] sheet = line.split(cvsSplitBy);    //comma as separator
                if(isNumeric(sheet[3])){                    //parse into integer, only if first col is num 
                    int n = Integer.parseInt(sheet[0]);
                    int pop = Integer.parseInt(sheet[3]); 
                    System.out.println(sheet[4]);
                    System.out.println(sheet[4].getClass().getName()); 
                    Long lon  = Long.valueOf(sheet[4]); //THIS IS WHERE THE ERROR OCCURS

......skip

The Error is the usual NumberFormatException

Exception in thread "main" java.lang.NumberFormatException: For input string: "34.95" 

Any ideas? Thanks Aj

1

There are 1 answers

4
uiroshan On

Focus on the description of the exception.

For input string: "34.95" 

It has a Decimal value, which can't convert into a Long. You could either use Double.valueOf("34.95") or do a validation for non integer values.