Search code examples
javaintegeraddition

"If the number is contained in the text and is repeated, return the sum of the number for each time it is repeated. Otherwise return 0."


I want to make it so that the code only adds completely unique versions of the number I am trying to read (ie. 1 in 111 gives out 3, but 33 in 333 only gives out 33.), but when I try to output the result, instances like the latter example count up once every interval and add each instance counted (33 in 333 gives out 66 instead of 33, for example).

Here is my code so far:

public static int sumRepeat(int num, String text)
         {
             int sum = 0;
             String calc = Integer.toString(num);
             int value = text.indexOf(calc);
             for (int i = 0; i < text.length() - calc.length() + 1; i++) {
                 if (text.substring(i, i + calc.length()).equals(calc))
                    sum += num;
             }
             return sum;
         }

Could someone help me get it to where I do not run into this error and the code runs as intended? Thank you.

Edit: Apologies, but I have more test cases than I initially said I had. Here are all of my test cases:

sumRepeat(1, "I love CSCE111") returns 3
sumRepeat(12, "The bill is $12.97") returns 0
sumRepeat(33, "333 bananas for 33 monkeys.") returns 66
sumRepeat(333, "My number is (333)-333-3333") returns 999
sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!") returns 0
sumRepeat(41, "41414141") returns 164
sumRepeat(0, "") returns 0

Solution

  • Update (based on updated question):

    You can use the Java Regex API to solve it. Use the regex, "(?<!\\$)(" + String.valueOf(num) + ")" and add each match to sum. Note that ?<! is used for negative lookbehind. Here, it means num should not be preceded by $.

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
        public static void main(String[] args) {
            System.out.println(sumRepeat(1, "111"));
            System.out.println(sumRepeat(33, "333"));
            System.out.println(sumRepeat(1, "I love CSCE111"));
            System.out.println(sumRepeat(12, "The bill is $12.97"));
            System.out.println(sumRepeat(33, "333 bananas for 33 monkeys."));
            System.out.println(sumRepeat(333, "My number is (333)-333-3333"));
            System.out.println(sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!"));
            System.out.println(sumRepeat(41, "41414141"));
            System.out.println(sumRepeat(0, ""));
        }
    
        public static int sumRepeat(int num, String text) {
            int sum = 0;
            Matcher matcher = Pattern.compile("(?<!\\$)(" + String.valueOf(num) + ")").matcher(text);
            while (matcher.find()) {
                sum += num;
            }
            return sum;
        }
    }
    

    Output:

    3
    33
    3
    0
    66
    999
    0
    164
    0
    

    Non-regex solution:

    You can use String#indexOf(String, int) to find the index of calc starting from the given index. Every time this index is found, add num to sum and move this index by the length of num; otherwise, keep moving this index by 1.

    Demo:

    public class Main {
        public static void main(String[] args) {
            System.out.println(sumRepeat(1, "111"));
            System.out.println(sumRepeat(33, "333"));
            System.out.println(sumRepeat(1, "I love CSCE111"));
            System.out.println(sumRepeat(12, "The bill is $12.97"));
            System.out.println(sumRepeat(33, "333 bananas for 33 monkeys."));
            System.out.println(sumRepeat(333, "My number is (333)-333-3333"));
            System.out.println(sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!"));
            System.out.println(sumRepeat(41, "41414141"));
            System.out.println(sumRepeat(0, ""));
        }
    
        public static int sumRepeat(int num, String text) {
            int sum = 0;
            String calc = Integer.toString(num);
            int len = calc.length();
            int i = 0;
            while (i < text.length()) {
                int index = text.indexOf(calc, i);
                if (index != -1) {
                    if (index == 0 || text.charAt(index - 1) != '$') {// Check to exclude num preceded by $
                        sum += num;
                        i = index + len;
                    } else {
                        i++;
                    }
                } else {
                    i++;
                }
            }
            return sum;
        }
    }
    

    Output:

    3
    33
    3
    0
    66
    999
    0
    164
    0
    

    Original solution:

    Modify the loop counter to terminate with i < text.length() and proceed with step value of num.

    public class Main {
        public static void main(String[] args) {
            System.out.println(sumRepeat(1, "111"));
            System.out.println(sumRepeat(33, "333"));
        }
    
        public static int sumRepeat(int num, String text) {
            int sum = 0;
            String calc = Integer.toString(num);
            for (int i = 0; i < text.length(); i += num) {
                if (text.substring(i, i + calc.length()).equals(calc)) {
                    sum += num;
                }
            }
            return sum;
        }
    }
    

    Output:

    3
    33