4
  • This method calculates the sum of all digits in a String of length 10. The String has to be of the form "12345?789x" or "12?4567890", where '?' can lie anywhere and has a value of 0, 'x' (if present) lies at the end of the String and is equal to 10.
  • The sum should be calculated as follows: For "11432?789x", sum = (10*1)+(9*1)+(8*4)+(7*3)+(6*2)+(5*0)+(4*7)+(3*8)+(2*9)+(1*10) = 164.
  • This code works perfectly for numbers ending with 'x', but for those that don't, it returns the value of sum as 0. For example, for "111?111111" instead of returning 48, it returns 0.
  • I'm not able to find out the error. Please help.

    public static int sum(String input,int l){
    int sum=0;
    int temp=0;
    char a;
    for(int i=0;i<l;i++){
        a=input.charAt(i);
        if(a=='x'){
            temp=10;
        }
         else if(a=='?'){
            temp=0;
        }
        else{
        temp = Character.getNumericValue(input.charAt(i));
        }
    
    
        sum = temp*(10-i)+sum;
    }
    return sum;
    }
    
ryan321
  • 63
  • 6
  • "12345?789x" or "12?4567890", where '?' can lie anywhere and has a value of 0, 'x' (if present) lies at the end of the String and is equal to 10." your example is not even of this form – Fabinout Jul 16 '15 at 13:10
  • 3
    "111?111111" from your code is result 48 – maskacovnik Jul 16 '15 at 13:11
  • @Fabinout it does, the numbers can be anything from 1 to 9, just not 0 and 10 (cause they are ? and x) – Manu Jul 16 '15 at 13:11
  • @Fabinout '?' can lie anywhere between positions 0 to 8. 'x', if present should only lie at position 9 (i.e. at the end). 'x' can be absent too. – ryan321 Jul 16 '15 at 13:13
  • @Manu so you iterate over your string and just multiply the first item by 10, the second by 9, etc? – Fabinout Jul 16 '15 at 13:15
  • @Fabinout, I didn't ask the question, but, yes. – Manu Jul 16 '15 at 13:16
  • @maskacovnik for "111?111111", result should be 48, but it returns it as 0. – ryan321 Jul 16 '15 at 13:16
  • 1
    No, your code results 48 @ryan321, make sure if `l` (length) is not `0` while passing it as parameter – maskacovnik Jul 16 '15 at 13:17
  • 1
    "111?111111" returns 48. Maybe you didn't set l to 10. – Manu Jul 16 '15 at 13:18
  • As the others are saying, `sum("111?111111")` is in fact returning 48. Maybe you should declare a variable `int l = input.length()` instead of taking it as a parameter? – marstran Jul 16 '15 at 13:20
  • @Manu : just replaced l by 10 (in the for loop), and its working now. Even though I passed l as 10 in the method argument.. Don't understand what happened. Thanks anyway. – ryan321 Jul 16 '15 at 13:22
  • Can you provide the code where are you calling this method @ryan321 – maskacovnik Jul 16 '15 at 13:23

4 Answers4

2

Your example:

111?111111

Expected result: 48

if I call:

String input = "111?111111";
int result = sum(input,input.length());

result is 48

Maybe you call method like this:

String input = "111?111111";
int result = sum(input,0);

There result is 0

Or second parameter of sum is corrupted.

maskacovnik
  • 3,009
  • 5
  • 18
  • 26
2

I wrote this test and it's green:

@Test
public void removeme() {
    String input = "111?111111";
    int sum = 0;
    int temp = 0;
    for(int i = 0; i < input.length(); i++){
        char a = input.charAt(i);
        if(a == 'x'){
            temp = 10;
        } else if(a == '?'){
            temp = 0;
        } else {
            temp = Character.getNumericValue(input.charAt(i));
        }

        sum = temp * (10 - i) + sum;
    }
    assertThat(sum , is(48));
}

I suggest you remove the argument l and just use input.length(), as I did.

Manu
  • 1,415
  • 11
  • 17
2

Funny though i m using the same code posted by you AND i am getting the correct answers. BTW the example cited by you (10*1)+(9*1)+(8*4)+(7*3)+(6*2)+(5*0)+(4*7)+(3*8)+(2*9)+(1*10) = 133. actually correct answer is 164.

Sidharth Dash
  • 233
  • 1
  • 4
  • 13
2

Simpler way:

public static int sum(String input) {

    int sum = 0, temp = 0, i = 0;

    for (char a : input.toCharArray()) {
        temp = a == 'x' ? 10 : a == '?' ? 0 : Character.getNumericValue(a);
        sum += temp * (10 - i++);
    }

    return sum;
}
Rajesh
  • 2,120
  • 1
  • 10
  • 14
  • Thanks a lot! But in terms of Code Quality, does it make much difference? – ryan321 Jul 16 '15 at 13:48
  • In code posted in question, `input.charAt(i)` is a repeated, recursive call and got removed here. With `foreach loop`, there is no need to pass, evaluate length of array. – Rajesh Jul 16 '15 at 15:01