0

This is the first time I am using Test Driven Development and I am making a small calculator but from strings to make it more interesting. The string is then broken down and the individual digits are counted up.

Unfortunatly I am running into this issue when adding two identical numbers. The sequence is 2,3,7,5,3. My first 3 is getting compaired to my last 3 which occupies the last position in the array. I make a continous check to see if the character is the last character in the array. But what I mean is checking the positioning and not the actual value itself.

Please note the strings are already trimmed, meaning they contain no spaces.

Before using arrays I had used the CharacterItorator. Unfortunatly I could not manage and decided if I could get the answer with trusty arrays. I managed to solve other sequences, but with non-repeating numbers as the last digit.

Input in string: "2,3,7,5,3". The answer should be 20, but its resulting in 23

public int inputIterator(String input){
        int currentCount = 0, tempCount;
        String currentString = "";
        char[] chars = input.toCharArray();
        for (char ch : chars){
            if(ch != ','){
                // it is a number
                if(ch == chars[chars.length-1]) {
                    currentString = currentString + ch;
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    currentString = currentString + ch;
                }
            } else {
                // It is a ','
                if(ch == chars[chars.length-1]){
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    if(currentString != ""){
                        tempCount = Integer.parseInt(currentString);
                        currentCount = currentCount + tempCount;
                        currentString = "";
                    } else {
                        // do nothing
                    }
                }
            }
        }
        return currentCount;
    }

The test expects the result of this sequence to be 20. But the answer provided is 23, which is because of the repeating digit.

EDIT: Changed my LOGIC; Answers provided in the comments do indeed work - but I wanted to still attempt my own spin

public int inputIterator(String input){
        int currentCount = 0, tempCount;
        String currentString = "";
        int loopCounter = input.length();
        char[] chars = input.toCharArray();
        for(int i = 0; i < loopCounter; i++){
            if(chars[i] != ','){
                // it is a number
                if(i == loopCounter-1) {
                    currentString = currentString + chars[i];
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    currentString = currentString + chars[i];
                }
            } else {
                // It is a ','
                if(i == loopCounter-1){
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    if(currentString != ""){
                        tempCount = Integer.parseInt(currentString);
                        currentCount = currentCount + tempCount;
                        currentString = "";
                    } else {
                        // do nothing
                    }
                }
            }
        }
        return currentCount;
    }
OfficerChalk
  • 37
  • 2
  • 6
  • Not sure how this relates to testing, since the test seems to be doing its job perfectly well. – jsheeran Oct 10 '19 at 16:01
  • @jsheeran You are right! I will see how to remove the tag. – OfficerChalk Oct 10 '19 at 16:04
  • `if(ch == chars[chars.length-1]) {` is the line causing your problem. It triggers any time a character is the same as the last character in `chars`, instead of at the last character in `chars`. It's not that easy to remedy this without changing the nature of your `for-each` loop to a `for` loops, so I'd recommend the following instead: `return Pattern.compile(",").splitAsStream(input).mapToInt(Integer::parseInt).sum();` – Avi Oct 10 '19 at 16:21
  • @Avi Thank you very much. I am a bit disappointed I couldn't solve it with my logic, but I never thought I would have this issue before. – OfficerChalk Oct 10 '19 at 16:28
  • @OfficerChalk Instead of checking for whether the character is *equal* to the last one, you can just do processing as soon as you exit the loop. As long as you keep track of the last visited character, you will have that knowledge when you exit the loop. – Avi Oct 10 '19 at 16:30
  • @Avi The only reason I am comparing my numbers to the last one in the index is to see if that selected digit 'ch' is the last digit in the array. But I am comparing their contents and not their position within the array, which is what I want to do but I cannot figure out. – OfficerChalk Oct 10 '19 at 16:34
  • @Avi You are right in that its a problem from the logics point of view. I have closed this thread but I will be amending my logic to see if I could have solved it by myself if I tried harder. Thank you very much. – OfficerChalk Oct 10 '19 at 16:37
  • Aside from main problem `if(currentString != ""){` -> [How do I compare strings in Java?](https://stackoverflow.com/q/513832) or in this case `if(!currentString.isEmpty()){..}` is more readable. – Pshemo Oct 10 '19 at 17:44

1 Answers1

0

The issue must be with your logic then. Would you consider using a shorter function based on the stream? For instance:

public int sumFromString(String input) {
    return Arrays.stream(input.split(","))
        .mapToInt(Integer::parseInt)
        .sum();
}
Fossan
  • 76
  • 1
  • 9