Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to solve this question in the below but I don\'t know what is wrong with

ID: 3937027 • Letter: I

Question

I have to solve this question in the below but I don't know what is wrong with my code. Please scroll down so you can see my code.

Q. Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of:

1. the number of characters in the sequence

2. the repeating character

If a character does not repeat, it should be left alone.

For example, consider the following string:

In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w" characters. "5e" represents 5 consecutive lowercase "e" characters, etc.

Write a method called compress that takes a string as input, compresses it using RLE, and returns the compressed string. Case matters - uppercase and lowercase characters should be considered distinct. You may assume that there are no digit characters in the input string. There are no other restrictions on the input - it may contain spaces or punctuation. There is no need to treat non-letter characters any differently from letters.

..................

< MY CODE >

public class Compress{

  public static String compress (String original) {

int count = 1;

String stringcp = "";

for (int i =0; i<original.length(); i++) {

     char a =  original.charAt(i);

while ( original.charAt(i) == original.charAt(i+1) &&  (i+1)<original.length() ) {

   count++;

i ++;

if (count >1) {

     stringcp = stringcp + String.valueOf(count) +String.valueOf(a);

}   else {

     stringcp = stringcp + String.valueOf(a); }

}  } return stringcp;

}   

public static void main(String[] args) {

       System.out.print("Enter the string: ");

       String original = IO.readString();

  IO.outputStringAnswer (compress(original));

   } }

      

      

Explanation / Answer

public class Compress1

{

public static void main(String[] args)

{

System.out.println("Enter a string");

String input = IO.readString();

if (input == null || input.length() == 0)

{

System.out.println("null or empty string input");

System.exit(0);

}

if (input.length() == 1)

{

System.out.println(String.valueOf(curr));

System.exit(0);

}

input += " ";

char curr = input.charAt(0);

int count=1;

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

{

char next = input.charAt(i);

if (curr != next)

{

if (count > 1)

{

System.out.print(count + String.valueOf(curr));

}

else

{

System.out.print(curr);

}

count = 1;

}

else

{

++count;

}

curr = next;

}

}

}