0

I need this code to compress and decompress a string given by the user, to compress the user will type either "java compression -c" or just "java compression". To decompress, it will be "java compression -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.

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);
            System.out.println(compressedString);
        }
        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);
                }
            }
        }
    }

    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);
                }
            }   
        }
    }
}
gdhc21
  • 107
  • 6
  • 18

2 Answers2

1

You have forgotten the type argument for your method definitions:

public static void decompress(compressedString)

Should be

public static void decompress(String compressedString)

Or even better

public static void decompress(final String compressedString)
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

There are two things going wrong in your code(As far as I can see):-

  1. public static void decompress(compressedString) should actually be public static void decompress(**String** compressedString). You have to tell the method that the argument it is going to receive is of String type.

  2. In your compress and decompress methods you have while (index < input.length());. Check ; at the end. Do you really want that. You will never execute the while loop because you are terminating it before it even begins.

Prateek
  • 1,916
  • 1
  • 12
  • 22