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

There\'s something wrong with my code. In the case of qwwwwwwwwweeeeerrtyyyyyqqq

ID: 3759910 • Letter: T

Question

There's something wrong with my code. In the case of qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT, it should return q9w5e2rt5y4qw2Er3T.

My code is as follows (the method name must remain the same):

public class Compress{
   public static void main(String[] args) {
       System.out.println("Enter a string.");
       System.out.println(compress(IO.readString()));
   }
  
   public static String compress(String original){
       String compressed = "";
       char i = 0;
       int counter = 1;
  
       for (int k = 0; k < original.length(); k++){
           if (i == original.charAt(k))
               counter++;
           else{
               compressed = compressed + i;
               if(!(counter == 1))
                   compressed = compressed + counter;
               i = original.charAt(k);
               counter = 1;
           }
       }
      
       compressed = compressed + i;
      
       if(!(counter == 1))
           compressed = compressed + counter;
      
       return compressed;
   }
}

Explanation / Answer

where is the statement to read input string
how can you print the compress string without input.
you take IO for input,but here you decalre output.
for example System.out.println("Enter minimum letters:");
problem with if statement and for loop also.number of mistakes are in your program.totally modified your program.

public class Compress
{
public static void main(String[] args)
{
System.out.println("Enter string");
String input = IO.readString();

  
if (input == null || input.length() == 0)
{
System.out.println("enter null or empty string input");
System.exit(0);
}

if (input.length() == 1)
{
System.out.println(String.valueOf(current));
System.exit(0);
}


input += " ";

char current = input.charAt(0);
int counter=1;

for (int i=1; i < input.length(); ++i)
{
char next = input.charAt(i);
if (current != next)
{
if (count > 1)
{
System.out.print(count + String.valueOf(current));
}
else
{
System.out.print(current);
}
counter = 1;
}
else
{
++counter;
}

current = next;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote