I have a string with about 150 numbers like this String num = "64513246563........";
I am trying to add each digit of this string. So my idea was to split it into an array of ints and add them from there. I start by splitting it into a String array, then I try to convert it to an Int array. I get an unknown source error. Below is the code:
String[] strArray = num.split("");
int[] intArray = new int[strArray.length];
for(int i = 0; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);
}
And here is the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
Can anyone see what I'm doing wrong or is there a more efficient way of doing this?
////////////////////////////////
Thanks for your help everyone, it seems that splitting a string using .split("") creates an empty string at index 0. That was my main problem but there was lots of useful pointers on how to solve the problem more efficiently :) Thank you all for your input