Validating substrings are equal

Viewed 68

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

3 Answers

After looking over and testing possible issues, I believe the issue is as simple in the for loop declaration

Since you are indexing startingCharacters, and startingCharacters is 2 less than s in size, you should also take that into account when iterating through it. My change SHOULD resolve this

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);
        }
    }

    int finalStartingCharSum = startingCharSum % 11;
    int endingCharSum = Character.getNumericValue(endingCharacter.charAt(0));

    if (finalStartingCharSum == endingCharSum) {
        result = true;
    }

    return result;
}

startingCharacters is shorter than s, therefore i goes to far.

for (... i < s.length() ...)
startingCharacters = s.substring(...);
startingCharacters.charAt(i)

You're doing a couple things wrong.

First for the following:

String startingCharacters = s.substring(0, s.length()-2);

you should change the -2 to -1. This will ignore all but the last character which I presume is your check digit.

And you need to change this

for (int i = 0; i < s.length(); i++) {

to this

for (int i = 0; i < startingCharacters.length(); i++) {

And finally, you are not summing anything. Just assigning. And move the i outside of the getNumericValue method.

startingCharSum = Character.getNumericValue(startingCharacters.charAt(i) * i);

However most of the above could just be replaced with the following using the original string by simply iterating one less than the length and starting at 1 instead of 0 (multiplying by 0 will always result in 0 so why do it?).

startingCharSum = 0;
for (int i = 1; i < s.length()-1; i++) {
     startingCharSum += Character.getNumericValue(s.charAt(i)) * i;
}

Also I get false when I run this.

And note that when you multiply by the position, you are always multiplying by 0 at the first position. If that is correct, you can always start your loop at 1 since the product of the first digit will always be 0.

Related