I'm writing a program that compresses strings using run-length encoding.
E.g. for input "aaabbccccddd", output should be "a3b2c4d3".
Currently the program shows no output.
public static void main(String[] args) {
String words1 = "aabbbcccc";
String words2 = compress(words1);
System.out.println(words2);
}
private static String compress(String w1) {
StringBuilder w2 = new StringBuilder();
int k = 0;
for (int i = 0; i < w1.length(); ) {
k++;
if(i+1>w1.length() || w1.charAt(i) != w1.charAt(i+1)) {
w2.append(w1.charAt(i));
w2.append(k);
k = 0;
}
}
return ((w2.length() > w1.length())? w1: w2.toString());
}
Output shows nothing in IntelliJ and I can't figure out why!! Also tried without StringBuilder. Even tried without StringBuilder and changing the return type to void in case it was a problem with handling a String. Same, no result.