Click here to Skip to main content
15,611,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im having trouble coming up with a way too decompress a string in java, this is a basic java class im taking so it only requires basic commands, nothing too fancy. The objective it to able to type
C:\>java Compress -c aaaabbbbbcc 

in the command prompt and it will print a4b5c2 (like it is compress the argument string).

The other objective is to type
C:\>java Compress -d a5b7c4

and it will print aaaaabbbbbbbcccc (like it will decompress the argument string). The decompression is the issue Im having. Here is my code, any help i get is much appreciated.

Java
import java.util.*;
public class Compress{
	public static void main(String args[]){

		Scanner scan=new Scanner(System.in);
		String originalString = scan.nextLine();
		int number = scan.nextInt();
		
		if(args[0].equals("-c"))
		{
			System.out.println("compress");
            compress(originalString);
		}
	
		else if(args[0].equals("-d"))
		{
			System.out.println("decompress");
            decompress(originalString);
		}
		else{System.out.println("Compress program by Kelsey Faram");
		System.out.println("usage: java Compress [-c,-d] < inputFile > outputFile");
		}
	}
	
	
	public static void compress(String originalString)
	{
		int count = 0;
		char comp = originalString.charAt(0);
		for(int i=0; i < originalString.length(); i++){
		originalString.charAt(i);
		
		if(comp==originalString.charAt(i))
		count++;
		else{
			System.out.print(comp+""+count);
			comp=originalString.charAt(i); count=1;
		}	
		} System.out.println(comp+""+count);
	}
	
	public static void decompress(String originalString)
    {
		
	}
	
	
}
Posted
Updated 10-Jun-20 10:56am
v2
Comments
TorstenH. 20-Feb-14 4:51am    
made it readable.

1 solution

I will not give you the code, but will try to guide you in the right direction.

This seems to be a simple RLE type compression, so decompression is very simple.
You need to understand what the compress method does, once you do this, the decompress should be obvious.

The compress will scan the input and for each character it finds output the character, followed by a count. So decompressing should be simple

1. take a character (x) from the string,
2. take the next character and convert to an int (y)
3. Output the character (x), (y) number of times

Converting a character to an int in this case is really simple, using basic ASCII, the character 0 has an ASCII value of 48, 1 has a value of 49 etc, so :

int count = originalString.charAt(i) - 48;


Also not that this only works if you never have more than 9 character repetitions in your string. It might be usefull to change the compress function to take that into account so that if you have a sequence like aaaaaaaaaaaaaa, the outpput will be a9a5.
 
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