idkwhatimdoing idkwhatimdoing - 2 months ago 23
Java Question

Run-length Encoding Program Issue

I'm new to Java and programming in general... I'm having an issue implementing a run-length encoding program where a string from user input (for example: KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG) would be encoded based on a flag character they input. So that particular string would look like this: $K13BCC$D15$K5MNUUU$G5

Could someone help me figure out what's wrong? I keep getting this as my output: $K13BCC$D14$K4MNUUU

It seems my program skips the last letters and has one less in some of them. Any help would be appreciated!

I apologize for my messy code. I know it's not that great. Just trying to figure out what's wrong here...

import java.util.Scanner;

public class RunLengthEncoding {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter input string: ");
String inputString = input.next();

System.out.print("Enter flag character: ");
String flagCharacter = input.next();

Boolean validInput = false;

for (int i = 0; i < inputString.length(); i++) {
if (Character.isUpperCase(inputString.charAt(i))) {
validInput = true;

} else {
System.out.print("Bad input.");
}

}

char repeatedLetter = inputString.charAt(0);

if (validInput) { // if the input is valid, continue
int counter = 0; // set counter equal to 0
for (int i = 0; i < inputString.length(); i++) { // iterate through the input string

if (repeatedLetter == (inputString.charAt(i))) { // if the current letter is equal to the next
counter++; // increment the counter
repeatedLetter = inputString.charAt(i); // set the repeated letter to the next letter
} else { // if the current letter is not equal to the next letter
if (counter > 3) { // and if the counter is greater than 3
System.out.print(flagCharacter + repeatedLetter + counter); // print the encoded string
} else { // if the counter is not greater than 3
for (int j = counter; j >= 0; j--) { // for every number in counter
System.out.print(repeatedLetter); // print the current repeated letter for as many times as it appears
}

}
repeatedLetter = inputString.charAt(i); // set the new repeated letter
counter = 0; // reset counter to 0

}
}

}

}

}

Answer

The for loop state is not quite the same for the initial character count and subsequent character count.

with your code the easiest fix i found was to change the for loop

for (int i = 1; i < inputString.length(); i++) { // start at 1 not 0

change the counter initialisation to 1 instead of 0 in both cases.

int counter = 1; // set counter equal to 1

and had to change the individual character output

for (int j = counter; j > 0; j--) { // for every number in counter

also the last character run was not being output.

giving

private static void process(String inputString, String flagCharacter) {

    // init counter state
    char repeatedLetter = inputString.charAt(0);
    int counter = 1;

    for (int i = 1; i < inputString.length(); i++) {

        if (repeatedLetter == (inputString.charAt(i))) {
            // match so update counter
            counter++;
        } else {
            if (counter > 3) {
                // counter needs to be above 3 to make worthwhile
                System.out.print(flagCharacter + repeatedLetter + counter);
            } else {
                // otherwise we will just output raw 
                for (int j = counter; j > 0; j--) {
                    System.out.print(repeatedLetter);
                }

            }
            // and re init our counter
            repeatedLetter = inputString.charAt(i);
            counter = 1;
        }

    }

    // output last character run
    if (counter > 3) {
        System.out.print(flagCharacter + repeatedLetter + counter);
    } else {
        for (int j = counter; j > 0; j--) {
            System.out.print(repeatedLetter);
        }
    }

}