Java string to int

I have a string of about 150 numbers, such as string num = "64513246563.....";

I'm trying to add every number of this string So my idea is to divide it into a set of ints and add them from there I first split it into a string array and then try to convert it into an int array I received an unknown source error Here are the codes:

String[] strArray = num.split("");
int[] intArray = new int[strArray.length];
        for(int i = 0; i < strArray.length; i++) {
           intArray[i] = Integer.parseInt(strArray[i]);
           }

This is an 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 did wrong or is there a more effective way?

////////////////////////////////

Thanks for your help, it seems to use Split ('') splitting a string creates an empty string at index 0 This is my main problem, but there are many useful pointers to solve the problem more effectively:) thank you for your comments

Solution

int sum = 0;
int sum = 0;
for (int i = 0; i < string.length(); i++) {
    sum += Character.getNumericValue(string.charAt(i));
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>