1

I'm trying to convert some strings from an array to integers using parseInt(). I am reading in lines from many separate files which look like this:

car,house,548544587,645871266

I have something like the code below:

String [] tokens = line.split(",");
try {
        line = line.trim();
        int a = Integer.parseInt(tokens[2]);
        int b = Integer.parseInt(tokens[3]);
        int c = (b - a);
        System.out.println(c);
        } catch (NumberFormatException e) {
                    System.out.println(e);
            }

But this fails with errors like this, for each line I read in:

java.lang.NumberFormatException: For input string: "548544587"
java.lang.NumberFormatException: For input string: "645871266"

Any idea what I might be missing?

0

3 Answers 3

5

You need to remove the quotes before splitting. It fails to convert "number" to an actual number because of quotes.

String line = "\"car\",\"house\",\"548544587\",\"645871266\"";
String[] tokens = line.replace("\"", "").split(",");
try {

    int a = Integer.parseInt(tokens[2]);
    int b = Integer.parseInt(tokens[3]);
    int c = (b - a);
    System.out.println(c);
} catch (NumberFormatException e) {
    System.out.println(e);
}

Output:

97326679
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, I had the line example wrong that I read in. It actually doesn't include quotes anywhere. I've updated my original post to show this. (Which is why this is stumping me...)
@gppanter i think it's an major update.. did you try this String [] tokens = line.trim().split(","); ?
@gppanter why you failed to ask about this on very first?
I did try that as well but get the same exceptions thrown. Trimming first doesn't seem to help.
trim after splitting int a = Integer.parseInt(tokens[2].trim());
|
0

Using a String for line does not produce an error for me, so it must be tied to what you're reading from the file. Also of note, you trim the line, but not each element in tokens. Try trimming each element, e.g., int a = Integer.parseInt(tokens[2].trim());. Also, you might be getting carriage return characters like \n and/or \r, so try to replace those before the split, e.g., String[] tokens = line.replaceAll("\\n", "").replaceAll("\\r", "").split(",");. Hope this helps.

2 Comments

Hmm, that didn't help either...I thought it would! Interestingly though, if I changed my lines to use 'floats' instead, it worked, insofar as it returned an integer.
That is very interesting. What I would do next is check each character in the token, which might lead you to the error. char[] aa = tokens[2].trim().toCharArray(); for(char c : aa) { System.out.print(c + " "); System.out.println(Integer.parseInt(""+c)); } , but that's just what I would do.
0
public static void main(String[] args) {
        // TODO Auto-generated method stub
        String line="car,house,548544587,645871266";
        String [] tokens = line.split(",");
        try {
                line = line.trim();             
                int a = Integer.parseInt(tokens[2].trim());
                int b = Integer.parseInt(tokens[3].trim());
                System.out.println(a+ " "+b);
                int c = (b - a);
                System.out.println(c);
                } catch (NumberFormatException e) {
                            System.out.println(e);
                    }

    }
    Output;
    548544587 645871266
    97326679

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.