Compres string method isn't returning a new string?

I'm trying to make a method that compresses a string. For example, the method would take "ttttesst" and return "4te2st."

When I run the method and print the result, I get: ""

public class Compress {

public static String compress(String original){
    int count = 1;
    int oglength = original.length()-1;
    StringBuilder newword = new StringBuilder("");
    for(int i = 0; i < oglength; i = i+count){
        count = 1;
        for(int k = 1; k < oglength-k-i; k++){
            if(original.charAt(i) == original.charAt(i+k)){
                count++;
                continue;
            } else if(original.charAt(i) != original.charAt(i+k) && original.indexOf(original.charAt(i+k)) - original.indexOf(original.charAt(i)) > 1){
                newword.append(newword);
                newword.append(count);
                newword.append(original.charAt(i));
                break;
            } else if(original.charAt(i) != original.charAt(i+k) && original.indexOf(original.charAt(i+k)) - original.indexOf(original.charAt(i)) == 1){
                newword.append(newword);
                newword.append(original.charAt(i));
                count++;
                break;
            }
        }
    }
    String returnword = newword.toString();
    return returnword;
}

2 answers

  • answered 2018-11-08 07:14 Wajih El Yaagoubi

    Compress method appends nothing in case of repetition with misuse of increment indices.

    Here is a working version based on your approach:

    public static String compress(String original) {
        int count;
        StringBuilder builder = new StringBuilder();
    
        for (int i = 0; i < original.length(); i = i + count) {
            count = 1;
            for (int k = i + 1; k < original.length(); k++) {
                if (original.charAt(i) == original.charAt(k)) {
                    count++;
                } else {
                    break;
                }
            }
    
            if (count > 1) {
                builder.append(count).append(original.charAt(i));
            } else {
                builder.append(original.charAt(i));
            }
        }
        return builder.toString();
    }
    

  • answered 2018-11-08 07:24 rdj7

    I think you are confused with your program as you are having unnnecessary loops that increases complexity and if-else ladder that compares characters that you can't keep track of in your mind (This is where debugging might help you).

    I don't understand what use case you are trying to get through but if you want exactly what you have mentioned above then following code snippet does it for you.

    Note: I have only taken basic scenarios in mind. For large number of test cases few small changes might be needed.

    public class Demo {
    
    public static String compress(String original){
        int count = 0;
        char temp = original.charAt(0);
        StringBuilder sb = new StringBuilder("");
        for(int i=0;i<original.length();i++) {
    
            if(original.charAt(i)==temp) {
                count++;
            }else {
                if(count!=1)
                    sb.append(count).append(temp);
                else
                    sb.append(temp);
    
                count=1;
    
                temp=original.charAt(i);
                continue;
            }
    
        }
        if(count!=1)
            sb.append(count).append(temp);
        else
            sb.append(temp);
        //Above four lines get last character 
        //and its occurrence if it's more than 1. 
    
    
        return sb.toString();
    }
    
    public static void main(String args[]) {
        System.out.println(compress("ttttesst")); 
    //Enter desired String value here.
    //Also you may make it user-interactive.
    }
    }
    

    Also you can refer to following links, Count number of occurrence of characters and HashMap Implementation and Hashing used for counting occurrence of character to get better understanding how to approach problems like this.

    I hope this helps !