Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run Length Compression

Posted on 2009-04-02
7
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 1000 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

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.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

722 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