-3

This is my function which should add and return the sum of the digits in a string.

public static int Sum(int a) {

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

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

    return sum; 
}

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.

Input: 8576 Output: 218 Expected output: 8+5+7+6 = 26

Fixed:

public static int Sum(int a) {

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

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

        }
    return sum;
}
Nissa
  • 4,636
  • 8
  • 29
  • 37
Emolk
  • 33
  • 1
  • 9
  • 2
    When asking for help with these sorts of things, always show sample input, the expected sample output, and the output you're currently getting that you don't understand. [More here.](/help/how-to-ask) – T.J. Crowder Nov 15 '16 at 13:26
  • 5
    simply because `'0' != 0` – AxelH Nov 15 '16 at 13:27
  • Input: 8756, 1, 58, 2, 0 Output: 218, 49, 109, 50, 48 – Emolk Nov 15 '16 at 13:27
  • 2
    Update your question instead of putting out more comments. And then: understand that you cant simply add **char** and **int** values here. The char `0` has not the same numerical value as the int 0. – GhostCat Nov 15 '16 at 13:28
  • Possible duplicate of [How to sum digits of an integer in java?](http://stackoverflow.com/questions/27096670/how-to-sum-digits-of-an-integer-in-java) – AxelH Nov 15 '16 at 13:29
  • [Could look at this one](http://stackoverflow.com/questions/21937091/how-to-calculate-sum-of-all-numbers-in-a-string) based on the current title : – AxelH Nov 15 '16 at 13:34

4 Answers4

3

The issue is that b is the Unicode value for the digit,¹ not the digit value. The Unicode value of the digit 1, for instance, is 49.

To sum up the digit values, you'll need to handle that. You could do that by using the method Tariq mentions in his answer, converting the character to a string and parsing it as an int, or using math based on the Unicode value I mentioned above.


¹ More pedantically, the value of the UTF-16 serialization of the digit if the character can be represented with a single code unit in UTF-16 (which the digits can). In the case of the digits (and all the 127 ASCII values), that's the same as the Unicode code point for the digit.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

like above stated int b is getting ascii value of character,

Change

int b = Row.charAt(i);

To

char c = Row.charAt(i);
int b= Character.getNumericValue(c);
henrybbosa
  • 1,139
  • 13
  • 28
1

int b = Row.charAt(i); is the problem. the integer gets the ascii value of the character. working code is :

public static int Sum(int a) {
        int sum = 0;
        int rest = 1;
        while(rest!=0){
            rest = a % 10;
            a = a / 10;
            sum = sum + rest;           
        }

        return sum;
    }

rest is always the last digit and since int is always a non-decimal dumber and rounds automatically you can can use a = a / 10; to "delete" the last digit

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • 1
    There is a problem with your answer, there is a huge difference between the beginning telling where is the problem and the code using a totaly different solution. Just adding `b -= '0'; //48` would have been enought. Add some spaces to make it more obvious ;) – AxelH Nov 15 '16 at 13:38
  • @AxelH Don't need the code, studying for an exam :) This method is also helpful for other questions so learning this way will help me a lot for my exam! – Emolk Nov 15 '16 at 13:46
  • @Emolk Then you should try to understand how to convert `'0'` into is numerical value... – AxelH Nov 15 '16 at 13:51
0

You are actually adding the Unicode value of the char not the numeric value. To do this take a look this method Character#getNumericValue from JDK Character class. You can change the code as following ::

public static int sum(int a) {
    String Row = String.valueOf(a);
    int counter = 0;
    int sum = 0;

    while (counter < Row.length()) {
        int b = Character.getNumericValue(Row.charAt(counter));
        sum = sum + b;
        counter++;
    }
    return sum;
}

also you can do this by manually. Instead of this int b = Character.getNumericValue(Row.charAt(counter)); line you can do int b = Row.charAt(counter) - '0'; It is like if you have char 1 and Unicode value of this is 49 and the Unicode value of 0 is 48. When you are subtracting with the 1 char value with the 0 then you will get the actual numeric value of the char 1.

seal
  • 1,122
  • 5
  • 19
  • 37