Q:

RLE Compression

I have the following java code for an RLE Compression algorithm:
public static String compress (String original) {
    String compressed = "";
    char letter = 0;
    int count = 1;
    for (int i = 0; i < original.length(); i++)  {
        if (letter == original.charAt(i)) {
            count = count + 1;
        }
        else {
            compressed = compressed + letter;
            if(count != 1) {
                compressed = compressed + count;
            }
            letter = original.charAt(i);
            count = 1;
        }
    }
    compressed = compressed + letter;
    if(count != 1) {
        compressed = compressed + count;
    }
    return compressed;
}

If the input is aaaawwwweerrr
The output should be: 3a4w2e3r 
The actual output is: a3w4e2r3
I tried switching 
compressed = compressed + count

to 
compressed = count + compressed

but when I do that the output is: 244 awer3
How should I modify the code so that the output is in the appropriate sequence?

A:

if(count != 1){
  compressed = compressed + count;
}
compressed = compressed + letter;

First append the count and then letter. 
This you need to do at two places, first inside the loop and second after the loop completes for the last character.
I would suggest you make use of StringBuilder.append() instead of String concatenation.
public static String compress (String original) {
  StringBuilder compressed = new StringBuilder();
  char letter = 0;
  int count = 1;
  for (int i = 0; i < original.length(); i++) {
    if (letter == original.charAt(i)) {
      count = count + 1;
    }
    else {
      compressed = count !=1 ? compressed.append(count) : compressed;
      compressed.append(letter);
      letter = original.charAt(i);
      count = 1;
    }
  }

  compressed = count !=1 ? compressed.append(count) : compressed;
  compressed.append(letter);
  return compressed.toString();
}

Input : aaaawwwweerrr
Output: 4a4w2e3r

