# Decompressing a String Recursively.

Jonathan Carter
Greenhorn
Posts: 3
Hello everyone,

I'm taking an intro class for computer science and I am stuck on this problem. I have to decompress a string recursively (no loops allowed at all).

For example, consider the following string:

qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
After applying the RLE algorithm, this string is converted into:

q9w5e2rt5y4qw2Er3T

I now have to take that string above and change it back into the original. So far I have

My problem is that when testing it with all letters (to test the second part out), I get an out of bounds exception on line 15. I know that it is most likely due to string length and that I am not stopping it in time so there is no substring of 1. Am I looking at this correctly and could anyone give some advice?

Also on the third section I am holding off to work on that until I get the second part down, so I just have a null for purposes of testing.

Thanks,

Paul Clapham
Sheriff
Posts: 21113
32
I assume the exception is being thrown by this statement?

If that's the case, then have a look at the API documentation for the String class. It does explain what conditions cause the substring() method to throw that exception, so you should fix your code so those conditions don't arise.

Winston Gutkowski
Bartender
Posts: 10422
63
Jonathan Carter wrote:I'm taking an intro class for computer science and I am stuck on this problem. I have to decompress a string recursively (no loops allowed at all).

What, absolutely none? Even for generating the output Strings? Phew. (Fun exercise though )

For example, consider the following string:
qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
After applying the RLE algorithm, this string is converted into:
q9w5e2rt5y4qw2Er3T

Personally, I'd be much more worried about something like:
qwwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
which presumably would be converted to:
q10w5e2rt5y4qw2Er3T
or is there some rule that you haven't told us about?

However, my suggestion (as always) is: StopCoding (←click).

Sit down and write out the steps required to process that encoded String - all of them, and in English (or your native language) - and don't write another line of Java code until you understand them forwards, backwards and sideways.

The next part of your problem is the "recursion/no loops" restriction. You'll need to analyse your list of steps (which will probably involve a loop of some sort) and work out what information you'll need to "roll" the body of that loop into a recursive call - and I warn you: unless you have the kind of mind that works that way, it's tricky (I still find it tough for non-trivial stuff after 35 years in the biz ).

Once you've done that, coding it should be a doddle - and that's the idea.

Winston

Jonathan Carter
Greenhorn
Posts: 3
Winston Gutkowski wrote:
Jonathan Carter wrote:I'm taking an intro class for computer science and I am stuck on this problem. I have to decompress a string recursively (no loops allowed at all).

What, absolutely none? Even for generating the output Strings? Phew. (Fun exercise though )

Personally, I'd be much more worried about something like:
qwwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
which presumably would be converted to:
q10w5e2rt5y4qw2Er3T
or is there some rule that you haven't told us about?

However, my suggestion (as always) is: StopCoding (←click).

Yep, no loops. And yes, I did forget to tell you that we are assuming the number before a letter will be no higher than 9. I've actually fixed that part by simply changing my code at the top to when the length is <=1 . And also, that link doesn't work.

I'm having problems on the third part though. I can't seem to wrap my head around it.

In theory, if it starts with a number I want to print out what is at length(1), and then decrement the number at length(0) until it is at 0, because well then the code beforehand would resolve it. I know why what I have coded so far isn't working, but I seem to have hit a road block. Any ideas/suggestions?

It's printing out yo160otf when I plug in "yo2otf".

Paul Clapham
Sheriff
Posts: 21113
32
I don't understand the code between lines 19 and 23. And there's no comments in the code to explain it. So could you explain it? (That might help you to understand why it's going wrong, or if not then it might help somebody else to see the problem.)

Jonathan Carter
Greenhorn
Posts: 3
Paul Clapham wrote:I don't understand the code between lines 19 and 23. And there's no comments in the code to explain it. So could you explain it? (That might help you to understand why it's going wrong, or if not then it might help somebody else to see the problem.)

Hope this helps.

Winston Gutkowski
Bartender
Posts: 10422
63
Jonathan Carter wrote:Hope this helps.

Not really - for the simple reason that I can't (or don't want to) read it.

I'd correct them myself, but there are LOTS of them (and on multiple panes).
My suggestion: Use the 'Edit' button and do what the link above suggests (and do it to ALL your posts that have long lines).

Winston

Paul Clapham
Sheriff
Posts: 21113
32
It didn't really help me either. What is "spot 1" and what is "spot 2"? And that's just one example of my confusion.

I've found I work better when looking at examples. So show me an example of what that code is working with, and what it is supposed to do with it.

Chris Klinchik
Greenhorn
Posts: 1

Try this

Campbell Ritchie
Sheriff
Posts: 49367
62
Welcome to the Ranch Chris Klinchik