# Object+recursion problem

• 01-26-2012, 03:38 PM
Mapisto
Object+recursion problem
Hi,

I've just recently learned recursions and I've got a program to write without using loops (meaning- using recursions instead).

Using a recursion in order to calculate a factorial is one simple thing, but when I've got to use it for every other thing, it gives me a headache :frusty:

One of the methods I wrote needs to get a string with products.. like this one : book\$10\$\$bottle\$20\$\$notebook\$30 (when \$ comes before the price and \$\$ comes before the name of the product- except the first and last).

then it's supposed to take that string, determine how many products are in it, and create an array of "product" objects with the exact number of products we got in the string.

Here's what I did:

Code:

```public static Product[] createArrayInsertProducts(String strProducts){                 int sum;                                 if (strProducts.length()<=1) {                         Product[] arrayProducts= new Product[sum];                         return arrayProducts;                 }                 else{                         if (!Character.isLetterOrDigit(strProducts.charAt(strProducts.length()-1))){ //a symbol.....                                 if(!(strProducts.charAt(strProducts.length()-1)==(strProducts.charAt(strProducts.length()-2)))){                                         sum++;                                         createArrayInsertProducts(strProducts.substring(0, strProducts.length()-1));                                 }                                 else{                                         createArrayInsertProducts(strProducts.substring(0, strProducts.length()-2));                                                                                 }                         }                         else{                                 createArrayInsertProducts(strProducts.substring(0, strProducts.length()-1));                                                                 }                                 }                                         }```
Here are my problems:
1) first, it gives me an error saying I need a return statement (haven't i got one ??)

2) it asks me to initialize "sum" . well, I can't write "sum=0" in the start of the method because then every time I use a recursion it will make it zero once again :|
but even when I dont initialize it, something still seems wrong, because every time I operate the method through recursion, it creates a new "sum"- it isn't a problem of memory, but I'm afraid it just won't keep the data from the first method call to the next.

3)Bringing an integer from the main method into the method above isn't possible, because they ask us not to ( in order to make our life more misrable ofc ).

I know that's long and I thank you for your help and patience, I'm a lost cause whenever it comes to recursions :(sweat):
• 01-26-2012, 03:54 PM
KevinWorkman
Re: Object+recursion problem
1- You have a return statement inside an if statement. What happens if the expression in that if statement evaluates to false? Hint: What do you want to do with the value returned from the second level (meaning the first time it's called by itself) of this method? The third level? Fourth?

2 and 3- Think about the direction you're going- are you working from the bottom up or the top down? Meaning, do you recurse all the way down and then come back up to find an answer, or do you keep a running sum as you recurse down the levels?
• 01-29-2012, 08:35 PM
Mapisto
Re: Object+recursion problem
Hi,
Sry it took me long to reply, had some problems with the connection.

I've considered what you wrote. You're right, I didn't give return statements to all the possibilies of the method, but what can i return if the method supposed to give an array back? shall I return an array of objects for every step of the recursion?

About my direction, I'm going from up to down. I've thought to maybe change it but couldn't figure why it would help me.

I'm still stuck at the same point trying to figure other oportunities... (considering your tips of course).. If I'm going in the wrong direction with my thinking please correct me :|

Thanks a lot!
• 01-30-2012, 03:51 PM
KevinWorkman
Re: Object+recursion problem
Well here's another way to look at it: you're returning something from your createArrayInsertProducts() method. But I see three different places where you just throw the value returned from the method away.
• 01-30-2012, 09:33 PM
Mapisto
Re: Object+recursion problem
Hi,

Well, I've found another way of accomplishing the same goal today.. by splitting the string every time it passes the # and then get the array length.

But still, if i wanted to solve it the way I did before, I see what you mean about not returning values from every possibility and because of that causing an error, but I couldn't just return back a new array of products for every possibility.. and even if i did, it wouldn't solve the problem because it won't be able to remember the same integer and add to it every time through the recursion.

I know the splitting way is the shortest and smartest, but is it the only way? couldn't i accomplish the same with my way ?