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);
}
}
}
}
}