Alright, I've been working on this one line for awhile, can't quite get it. I need to read all the characters after the "," (comma), ending at the end of the line. I'm guessing I'm just making a simple mistake. Can anyone help me out?

blocksArray [numBlocks][1] = Integer.parseInt(line.substring (line.indexOf(","),line.length()));

I thought substrings went something like this... line.substring (START,END)? Can I not use the length method for the end space?


I keep getting this error after I run the program..

Exception in thread "main" java.lang.NumberFormatException: For input string: ","
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at importCookies.main(importCookies.java:31)

Sure, you can use length. The parsing problem is that you are using the indexOf(",") value to start, when you should use indexOf(",")+1. You are getting the comma and it won't parse that.

Thanks a bunch, not sure how I didn't pick that one up.

Thanks again.

And when you are receiving an error, that you don't know in a line where you are calling a lot methods one inside another, try to break it a bit:

String stringToParse =  line.substring (line.indexOf(","),line.length());

System.out.println(stringToParse);

int num = Integer.parseInt(stringToParse );
blocksArray [numBlocks][1] = num ;

In that way it is easier to find your error: System.out.println(stringToParse);

This article has been dead for over six months. Start a new discussion instead.