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