run length encoding compressing

user10993577 :

So the point of this code is to compress a string and if it occurs more than 3 times, add a "# + number of occurence". For a letter the format is (#num)(letter) for a number value, they both have "#".

for example, if i have yyyy77777 the output would be #4y#5#7 but my problem is it doesn't print the number values with "#" in front of it, so my output it $4y#57

another problem i have is that when it's less than 3, it just prints the same. so i.e.: nnnnnff should have an output of #5nff but my code turns it into #5n2f

any help would be appreciated!

public class Compress {

public static void main(String[] args) {
    java.util.Scanner in = new java.util.Scanner(System.in);
    String s = in.next();
    String compressString = compress(s);
    System.out.println(compressString);

}
public static String compress (String original){
    String str = "";
   // int n = (Integer) null;
    int count = 1;
    for(int i=0; i<original.length()-1; i++){

        if(original.charAt(i) == original.charAt(i+1)){
            count++;
            if(i == original.length()-2){
                if(count == 1){
                    str = str + original.charAt(i);
                }
                //if count is equal or greater than 3, print with #
                else if (count >= 3) {
                    str = str + "#" + count +  original.charAt(i);
                }          
                else{
                    str = str + original.charAt(i);
                }                    
            }
        }
        else{
            if(count == 1){
                if(i == original.length() - 2){
                    str = str + original.charAt(i);
                    str = str + original.charAt(i+1);
                }
                else{
                str = str + original.charAt(i);
                }
            }
            else{
                if(i == original.length() - 2){
                    str = str + count + original.charAt(i);
                    str = str + original.charAt(i+1);
                }
                else{
                    str = str +"#"+ count + original.charAt(i);
                }


            }

            count = 1;
        }
    }
    return str;
} 

}

Sweeper :

Your code is currently treating digits and non-digits the exact same way. To fix your code, you just need to add if statements to check if the characters are digits.

You can add the if statements just before you append a #(count)(character) to the result.

Replace this line (there are two lines like this! Replace both of them):

str = str + "#" + count +  original.charAt(i);

with

if (Character.isDigit(original.charAt(i))) {
    str = str + "#" + count + "#" + original.charAt(i);
} else {
    str = str + "#" + count +  original.charAt(i);
}

Here is how I would solve this problem:

public static String compress (String original){
    String str = "";
    int count = 0;
    char currentChar = original.charAt(0);
    for(int i=0; i<original.length(); i++){
        if (currentChar == original.charAt(i)) { // count it if this not a new character
            currentChar = original.charAt(i);
            count++;
        } else { // if we have encountered a new character
            if (count > 2) { // and we have already counted 2 or more of the previous char
                if (Character.isDigit(currentChar)) {
                    str += "#" + count + "#" + currentChar;
                } else {
                    str += "#" + count + currentChar;
                }
            } else if (count == 2) { // if we have only counted two of the previous char
                str += currentChar;
                str += currentChar;
            } else if (count == 1) { // if we have only counted one of the previous char
                str += currentChar;
            }
            currentChar = original.charAt(i);
            count = 1;
        }
    }
    // treat the end of the string as a new char
    if (count > 2) {
        if (Character.isDigit(currentChar)) {
            str += "#" + count + "#" + currentChar;
        } else {
            str += "#" + count + currentChar;
        }
    } else if (count == 2) {
        str += currentChar;
        str += currentChar;
    } else if (count == 1) {
        str += currentChar;
    }
    return str;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=159887&siteId=1