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;
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With