Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

So, I'm attempting to fill an integer array with individual numbers found in a one thousand character string. I have an algorithm to do this, but currently, java keeps throwing the "java.lang.NumberFormatException" for the string. Could someone help me with this? (and yes, this is for project Euler) ill try to post my code

OpenStudy (anonymous):

Here is the .java file. The code currently has no use, but this is just the first step in the solution to this problem.

OpenStudy (rsmith6559):

Java isn't my best language, but 3 things come to mind: Does Integer.parseInt() want a string, or an array of char? Does Integer.parseInt() return and int or an Integer? Is the first character of string a the integer, or the linefeed? HTH

OpenStudy (anonymous):

Integers are 32 bits, which equates to about 10 decimal digits. You're trying to store numbers that are much, much larger than that so you're getting an error. If you want the individual character at location i, use "charAt();". If you want numbers that are really that big look up BigInteger (arbitrary precision numbers).

OpenStudy (rsmith6559):

I fiddled with it after I signed off last night, the call to a.substring() is wrong.

OpenStudy (anonymous):

From the Java API documentation: substring(int beginIndex) Returns a new string that is a substring of this string So in your very first call you are returning the original string, which is way too long, and most likely not what you want. This version of substring returns the whole string from beginIndex to the very end. You should also be careful in your choice of words; I think you don't want to fill the array with numbers, but with digits? In that case, you can either use: charAt(int index) Returns the character at the specified index. and cast to String Or use the other substring method, which allows the end to be specified, which, if you just want each digit, would just be c+1. So the call inside Integer.parseInt would be a.substring(c, c+1). And no, it doesn't cause any trouble at the end, because the second index is _exclusive_, i. e. it stops before it reaches that index. __ In general, I would try to be more careful with what I put into parseInt (although in this particular case it should work, if the for loop goes beyond the string's length, you'll get an IndexOutOfBoundsException. To make sure that this will never happen, base the loop on the length of a string instead of a constant (use a.length instead of 999). There is also an inconsistency. If the string really is 1000 characters long, you will miss the last character. If you use length as mentioned above, your array will be too small (size of 999 instead of 1000). Just base the size of the array on the string length too.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!