0

For my assignment, I have to be able to decompress a string recursively with no for loops. I'm having some trouble trying to limit myself from using for loops and I'd appreciate it if I could receive some assistance. Towards the end, I have a for loop and I was wondering if there was a way I could remove it with something else and still have my program do what I intend for it to do

public class StringRec {
    public static void main(String[] args) {
        System.out.println("What text do you want to decompress?");
        String compressedText = IO.readString();
        System.out.println(decompress(compressedText));
    }
    public static String decompress(String compressedText) {
        if (compressedText.length()<=1){
            return compressedText;
        }
        String first=""; 
        String rest=""; 
        char c = compressedText.charAt(0); 
        if (Character.isLetter(c) == true) {
            first = compressedText.substring(0,1); 
            rest = compressedText.substring(1); 
            return first + decompress(rest); 
        } else { 
            first = compressedText.substring(1,2); 
            rest = compressedText.substring(2); 
            int x = compressedText.charAt(0)-'0'; 
            char y = compressedText.charAt(1);
            String tst = "";

            for(int i = 0; i < x; i++) {
                tst = tst+y;
            }

            return tst + decompress(rest); 
        } 
    }
}
Dan Oberlam
  • 2,435
  • 9
  • 36
  • 54
CS111-Help
  • 13
  • 2

2 Answers2

1

Use a while loop to do the same thing.

int i = 0;
while(i < x) {
    i++;
    tst += y;
}

If you can't use loops altogether, then use recursion.

int i = 0;

public String recursiveAppend(String tst) {
    if(i >= x) {
        i = 0;
        return tst;
    }
    else return recursiveAppend(tst + y);
}

If you're using > Java 1.5, then use String tst = new String(new char[x]).replace('\0', y);. (from here)

Community
  • 1
  • 1
jfdoming
  • 975
  • 10
  • 25
0

Recursion to the rescue, bonus point if it is tail recursive:

 String tst = repeat(y, x, "");

...

private String repeat(y, x, b) {
    if (x == 0) {
        return b;
    }
    return repeat(y, x - 1, b + y) ;
} 
DRC
  • 4,898
  • 2
  • 21
  • 35
  • I don't think I'm allowed to do that with String tst. In Eclipse, I'm getting the error message "Cannot make a static method for the non-static method repeat(char, int, String) from the type StringRec" – CS111-Help Dec 09 '14 at 19:52
  • Also, yeah, it has to be all recursion, with no loops of any kind – CS111-Help Dec 09 '14 at 20:00
  • @CS111-Help sorry but I don't understand both of your comments. – DRC Dec 09 '14 at 21:35
  • Well for the first comment, in Eclipse, I'm getting that error message next to the line that contains String tst = repeat(y, x, ""); – CS111-Help Dec 09 '14 at 21:39
  • For the second comment, I'm just saying that my program has to be done through recursion, with no loops of any kind, AND I can only use one method – CS111-Help Dec 09 '14 at 21:40
  • ok I noticed now you are calling this from the main, make the method static or put that in a class where you make an instance, for the second comment, I'm not using any loop, do I? – DRC Dec 09 '14 at 21:40
  • I'm not allowed to use instance methods – CS111-Help Dec 09 '14 at 21:42
  • I was showing how to replace that for loop with a recursive algorithm, to achieve an equivalent thing removing all that part just use [StringUtils.repeat](https://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java) – DRC Dec 09 '14 at 21:46