Summing the digits of a string using charAt
<p>This is my function which should add and return the sum of the digits in a string.</p>

<pre><code>public static int Sum(int a) {

    String row = String.valueOf(a);
    int counter = 0;
    int sum = 0;

    while (counter&lt;row.length()){
        int b = row.charAt(counter);
        sum = sum + b;
        counter++;      
    }

    return sum; 
}
</code></pre>

<p>I'm not sure why this does not add all the digits of the integer. Output is giving me completely wonky answers. Help would be appreciated, cheers.</p>

<p>Input: 8576 
Output: 218
Expected output: 8+5+7+6 = 26</p>

<p>Fixed:</p>

<pre><code>public static int Sum(int a) {

    String row = String.valueOf(a);
    int counter = 0;
    int sum = 0;

    while (counter&lt;row.length()){
        String b = String.valueOf(row.charAt(counter));
        int c = Integer.parseInt(b);
        sum = sum + c;
        counter++;

        }
    return sum;
}
</code></pre>