user1191027
user1191027

Reputation:

Converting a String to an Integer

I'm trying to convert a String to an Integer. I have the following code:

List<String> strings = populateSomeStrings();
List<Integer> ints = new ArrayList<Integer>();

for (int i = 0; i < strings.size(); i++) {
    ints.add(Integer.valueOf(strings.get(i)));
}

When I run it I get an exception saying:

java.lang.NumberFormatException: Invalid int: "1000"

Any ideas why this would be happening? I also tried Integer.parseInt but it does the same thing.

Thanks

Upvotes: 2

Views: 371

Answers (1)

paxdiablo
paxdiablo

Reputation: 881223

There's obviously something in your strings that isn't numeric.

Catch the exception and print out the string length and code points for each character, using codePointAt for example.

That should tell you what's wrong.

Upvotes: 2

Related Questions