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