Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Run Length Compression

I have this for homework and I cannot figure out what I need to change to add spaces in between the output. I have tried everything I can think of can someone offer some help.
I included part of the code below.
Thanks

I am trying to set my output to
5 A, 1 B, 12 C, 3 D, 20 E

I currently get
5A1B12C3D20E
public static void main(String[] args) {
             RunLengthCompression RLC = new RunLengthCompression();
             String letter = "AAAAABCCCCCCCCCCCCDDDEEEEEEEEEEEEEEEEEEEE";
             System.out.printf(RLC.encode(letter));

Open in new window

0
navyretired
Asked:
navyretired
  • 4
  • 3
1 Solution
 
objectsCommented:
can u post your encode() method

0
 
navyretiredAuthor Commented:
Here it is. I did not post it before because I have someone using it in our class. I found my exact question and code from another assignment posted on another board from (I Think) someone in my class.
 public String encode(String source) {
        StringBuffer dest = new StringBuffer();
        for (int i = 0; i < source.length(); i++) {
            int runLength = 1;
            while( i+1 < source.length() && source.charAt(i) == source.charAt(i+1) ) {
                runLength++;
                i++;

Open in new window

0
 
objectsCommented:
thats not all of it, need to see the code to help you put spaces in the required spots :)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
navyretiredAuthor Commented:

public class RunLengthCompression {
 
    public String encode(String source) {
        StringBuffer dest = new StringBuffer();
        for (int i = 0; i < source.length(); i++) {
            int runLength = 1;
            while( i+1 < source.length() && source.charAt(i) == source.charAt(i+1) ) {
                runLength++;
                i++;
            }
            dest.append(runLength);
            dest.append(source.charAt(i));
        }
          
        return dest.toString();
    }
     public static void main(String[] args) {
             RunLengthCompression RLC = new RunLengthCompression();
             String letter = "AAAAABCCCCCCCCCCCCDDDEEEEEEEEEEEEEEEEEEEE";
             System.out.println(RLC.encode(letter));
             System.out.println();
           
    }
}

Open in new window

0
 
objectsCommented:
           dest.append(runLength);
            dest.append(' ');
            dest.append(source.charAt(i));
            dest.append(' ');

0
 
navyretiredAuthor Commented:
Worked like a charm thanks
0
 
navyretiredAuthor Commented:
Is there anyway to edit or delete this question?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now