Reputation: 1
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
}
}
}
}
}
Upvotes: 0
Views: 252
Reputation: 8259
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);
}
}
}
Upvotes: 1
Reputation: 1106
First, you should be setting your counter
variable to 1 in the outer else
block, since you are setting repeatedValue
right before it, and that counts as a single count of the letter. You also need to take into consideration the last sequence of characters in your string, so you need a final if
statement outside of your loop for when you hit the end:
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
} 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 = 1; // reset counter to 1
}
}
// We at the end of the string now, print everything else
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
}
}
Upvotes: 1