Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need this code to compress and decompress a string given by the user, to compress the user will type either "java practice -c" or just "java practice". To decompress, it will be "java practice -d". Any help would be appreciated. It will now compile and run; however, when you type in the string it will only print out compress and not the compressed string.

C#
import java.util.*;
public class practice
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        String originalString = scan.nextLine();
        String compressedString = "";
        int index = 0;
        int numReps = 0;
        char nextChar = ' ';
        if (args.length==0 || args[0].equals("-c"))
        {
            System.out.println("compress");
            String s = scan.next();
            compress(originalString);
        }
        else if (args[0].equals("-d"))
        {
            System.out.println("decompress");
            decompress(compressedString);
        }
        else
        {
            System.out.println("Error! Unknown mode.");
        }

    }

    public static void compress(String originalString)
    {
        int index = 0;
        int numReps = 0;
        char nextChar = ' ';
        while (index < originalString.length())
        {
            numReps = 0;
            char c = originalString.charAt(index);
            if (!Character.isDigit(c))
            {
                nextChar = c;
                index++;
            }
            else
            {
                while (Character.isDigit(c))
                {
                    int temp = Integer.parseInt(""+c);
                    numReps = temp;
                    index++;
                    if (index >= originalString.length())
                    {
                        break;
                    }
                    c = originalString.charAt(index);
                }
                for (int i =0; i<numReps; i++)
                {
                    System.out.println(nextChar+i);
                }
            }
        }
    }

    public static void decompress(String compressedString)
    {
        int index = 0;
        int numReps = 0;
        char nextChar = ' ';

        while (index < compressedString.length())
        {
            numReps = 0;
            char c = compressedString.charAt(index);
            if (!Character.isDigit(c))
            {
                nextChar = c;
                index++;
            }
            else
            {
                while (Character.isDigit(c))
                {
                    int temp = Integer.parseInt(""+c);
                    numReps = (numReps*10) + temp;
                    index++;
                    if (index >= compressedString.length()) break;
                    c = compressedString.charAt(index);
                }
                for (int i =0; i<numReps; i++)
                {
                    System.out.println(nextChar);
                }
            }
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 4-Oct-13 20:51pm    
Did you run it under the debugger, spot any particular problem? Otherwise it would not be a question, rather an attempt to have someone making you job for you.
—SA
Ron Beyer 5-Oct-13 0:03am    
It looks like you are trying to do some kind of run-length encoding of the data, which actually makes strings bigger than what they start out as. RLE works ok-ish for repeating binary data, like bitmaps, but not so much for strings because there are rarely more than 2 of any letter in a row. A majority are by themselves so you need to add a digit and the letter for each one. For example, the string "example" RLE compressed is 1e1x1a1m1p1l1e which is twice as long as it started out.

If you are wanting to compress large text files, you can do a dictionary (assign each word an ID, store the unique words in the header then just the ID's for the words in the file).

1 solution

Remove the line:
Java
String s = scan.next();

just before the call to compress.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900