Solved

Run Length Compression

Posted on 2009-04-02
7
198 Views
Last Modified: 2013-11-23
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
Comment
Question by:navyretired
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24055023
can u post your encode() method

0
 

Author Comment

by:navyretired
ID: 24055057
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
 
LVL 92

Expert Comment

by:objects
ID: 24055082
thats not all of it, need to see the code to help you put spaces in the required spots :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:navyretired
ID: 24055110

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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 24055122
           dest.append(runLength);
            dest.append(' ');
            dest.append(source.charAt(i));
            dest.append(' ');

0
 

Author Closing Comment

by:navyretired
ID: 31566022
Worked like a charm thanks
0
 

Author Comment

by:navyretired
ID: 24055318
Is there anyway to edit or delete this question?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 88
What is the latest versions eclipse neon 2 139
Unable to start eclipse ? 17 128
why cannot we forward request once the response is committed 2 33
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now