permaculture playing cards*
The moose likes Java in General and the fly likes Decompressing a String Recursively. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Decompressing a String Recursively." Watch "Decompressing a String Recursively." New topic
Author

Decompressing a String Recursively.

Jonathan Carter
Greenhorn

Joined: Apr 06, 2013
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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jonathan Carter
Greenhorn

Joined: Apr 06, 2013
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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Apr 06, 2013
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

Joined: Mar 17, 2011
Posts: 8244
    
  23

Jonathan Carter wrote:Hope this helps.

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

Please read the DontWriteLongLines.

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).

Either that or take pot luck on your answers...

Winston
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Apr 08, 2013
Posts: 1

Try this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
Welcome to the Ranch Chris Klinchik

Please explain what your else statement does.
I shall try adding code tags which you should always use; they often make the code look a lot better.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Decompressing a String Recursively.