Java Problem Compress What I write get 0 score public class
Java Problem Compress
What I write get 0 score.
public class Compress {
public static String compress (String original){
System.out.println(\"Enter a String: \");
original = IO.readString()+\'0\'; // add a digit after string makes it impossible to reach
int count = 1;
String output = \"\";
for (int a = 0; a < original.length()-1; a++){
if (original.charAt(a)== original.charAt(a+1)){
count++;
} else{
if (count == 1)
output = output + original.charAt(a);
else
output = output + count + original.charAt(a);
count = 1;
}
}
IO.outputStringAnswer(output);
return output;
}
public static void main(String[] args){
compress(\"\");
}
}
Write your code in the file Compress.java. Your code should go into a method with the following signature. You may write your own main method to test your code. The graders will ignore your main method: public static String compress (String original){} 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: the number of characters in the sequence 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 of9 \"w\" characters. \"5eSolution
Compress.java
public class Compress {
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println(\"Please Enter the String...\");
String s = in.next();
String compressString = compress(s);
System.out.println(compressString);
}
public static String compress (String original){
String str = \"\";
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);
}
else{
str = str + count + 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;
}
}
Output:
Enter the String:
qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
q9w5e2rt5y4qw2Er3T

