I am trying to take a string and validate the final digit to the rest of the string. I have made two substrings. for the first substring you have to times each char by its position add it to a count and then do modulus 11 of the result of the whole count. This should then equal the second substring. I am working with TDD and I am getting an error that the results are not equal when they should be. I am unsure why and any help would be great.
Here is my error: Error
Here is my method:
public boolean checkEndingDigit(String s) {
String endingCharacter = s.substring(s.length()-1);
String startingCharacters = s.substring(0, s.length()-2);
int startingCharSum = 0;
boolean result = false;
for (int i = 0; i < s.length()-2; i++) {
if (Character.isDigit(startingCharacters.charAt(i))) {
startingCharSum = Character.getNumericValue(startingCharacters.charAt(i) * i);
System.out.println(startingCharSum);
}
}
int endingCharSum = Character.getNumericValue(endingCharacter.charAt(0));
int finalStartingCharSum = startingCharSum % 11;
System.out.println(finalStartingCharSum);
System.out.println(endingCharSum);
if (finalStartingCharSum == endingCharSum) {
result = true;
}
return result;
}
Here is my test:
@Test
public void checkValidEndingDigit() {
boolean result = validator.checkEndingDigit("0471958692");
assertTrue(result);
}
Any help would be great! Thanks