Results 1 to 5 of 5
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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

    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:

    Java 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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 ?

Similar Threads

  1. Problem with recursion
    By fatabass in forum New To Java
    Replies: 12
    Last Post: 12-15-2011, 11:46 PM
  2. recursion problem
    By katiebear128 in forum New To Java
    Replies: 2
    Last Post: 10-20-2011, 08:19 PM
  3. recursion problem
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 01-05-2011, 03:45 PM
  4. Recursion problem
    By luke in forum New To Java
    Replies: 6
    Last Post: 10-06-2010, 07:35 AM
  5. Java Recursion Problem
    By gmnnn in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-06-2009, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •