1. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Confused about the result of method

Hello everyone, for a task at uni I need to explain why this program displays 3 for r, I can't seem to grasp what happens with the chars in the method and how this influences the value of r.

XML Code:
```public class Test {

static int m (char[] a,char x,int i,int r){
if (i>=a.length) return r;
else if (a[i]==x) return m(a,x,i+1, r+1);
else return m(a,x,i+1,r);
}

public static void main(String[] args) {
char [] c={'a','b','c','a','b','a'};
int r= m(c,'a',1,1);
System.out.println(+r);
}

}```
Last edited by JayCut; 11-01-2013 at 01:00 PM.

2. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Confused about the result of method

Step through it with a paper and pencil.
It should become clear which route it is taking.

3. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

Well I had done that and came to the conclusion that:

• a.length would be 6, so it wouldn't execute the 'if' as i=1 and thus not greater or equal than 6
• that a[i] would be 'b' so it also wouldn't execute the 'else if' as x='a'
• so it executes the last else and that would be m(c,'a',2,1)

but I really just don't know what happens when that method is executed and hoped someone would make me see it.

4. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Confused about the result of method

It's recursive, so it goes back into that method (m) with those new values. Well, only i has changed at this point.

5. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

I guess what you're saying is that the method is executed again with m(c,'a',2,1), could you explain why this happens and why it doesn't just stop and r stays 1?

So then the method is being executed another time when i becomes 3 and the 'else if' is executed making it m(c,'a',3,2).
Then this goes back into the method making it after two times when i becomes 5 m(c,'a',5,3).
After one more time i has become 6 and the first 'if' is executed after which it returns r which is now 3.

I think I can now answer my first question of this post, that the method will only stop after the condition for the 'return' is met, and as long as it isn't it will keep executing the method.
Is this correct?

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Confused about the result of method

You could always sprinkle some print statements in there to see what is happening.

Regards,
Jim

7. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

Originally Posted by jim829
You could always sprinkle some print statements in there to see what is happening.

Regards,
Jim
Would you mind giving me an example please, or some instructions on how to?
I've been trying to do this in some of my programming but I find it very difficult to place them in loops, methods and/or recursions.
In class they've told me that the debugger (we use Eclipse) is also a possibility to see what's going on with your variables, but again in loops etc. I find it hard to make it stop after say, one iteration, check the variables and then let it to do another.

8. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Confused about the result of method

Originally Posted by JayCut
Would you mind giving me an example please, or some instructions on how to?
Check out line 12 of the code you posted yourself!

9. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Confused about the result of method

Well, you already have one print statement there at the end. Just put in some more using different variables. Make certain you add {} to keep your
conditional blocks intact. And using the Eclipse debugger is a great idea. You can single step thru the program and watch all the variables update at your own
pace. This should be especially easy since you won't get bogged down in unrelated JDK method calls.

Regards,
Jim

10. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

I realise that line 12 is a print statement, but that will just give me the end result. I'm wondering how to print out variables that are the result of only one time going through the method for example, where I would have to put those. I've successfully used the debugger and been able to check it that way though.

Someone mind telling me if the explanation I gave to the method in post #5 was correct?

11. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Confused about the result of method

Yes, that's essentially what is happening.
It will keep calling itself (recursion) until the value of i passes that first if test, at which point it returns the value back up the call chain.
If the first if statement were to never be valid, then you'd eventually get a StackOverflow error as the system has a limit on the size of the call chain it can handle.

12. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

Awesome, thanks a lot for the help and making me see that!

13. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Confused about the result of method

Amazing that you managed to use a debugger to answer your question here, that deserves a congratulations. But then I have to wonder why someone with your awesome learning abilities fails to Google some simple examples about using something as trivial as println().

If you are still wondering:

Java Code:
`System.out.println("VAR 1: " + var1 + ", VAR 2: " + var2 + ", VAR 3: " + var3);`
As an example. There are always multiple solutions to the same problem. and this has nothing to do with println() specifically, it is just basic Java String logic.

14. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

Not sure if I'm completely not seeing it or if I'm being misunderstood.
Essentially what I want to do is let my program, in my specific example, print the value of r after one time going through the method, which -according to my pencil and paper method and according to my debugger- should be 1, then I'd want to print r after two times which would still be 2, after three times =3, etc.
Because all I'm getting now on line 12 is the end result of r.

15. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Confused about the result of method

Eh no, that is probably my mistake then.

You don't want to (only) print r, you want to print whatever you calculate inside the m() method (please, use more meaningful names). You can't do that with your current code as you immediately return it from the method, you'd have to capture the result in a variable, print it, then return the variable.

Java Code:
```int var = calculateStuff();
System.out.println("VAR: " + var);
return var;```
I tend to do that anyway, because having the intermediate result stored in a variable makes it that much easier to see the content of the variable in a debugger before it is returned.

16. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Confused about the result of method

Just stick a call to println in the first line of the method, so it will print out whatever each time the method is called.
Getting it to show 'i' and 'r' will show how the recursion is working.

17. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

Sorry for the confusion, writing the print statements for r and i immediately in the beginning of my method made it work, this will certainly help me in the future.
Thanks everybody!

18. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Confused about the result of method

I am not certain what you do or do not understand so a simpler example may clear up some stuff.
This just prints out the numbers 1 to N. Print statements are included to illustrate recursive calls.
If you find it too busy or confusing in the printout department, just comment some of them out.

Java Code:
```public class Recurse {
public static void main(String[] args) {
printOneToN(10);
}

public static void printOneToN(int n) {
System.out.println("printOneToN entered: n = " + n);
if (n > 1) {
System.out.println("n still greater than 1 (n == " + n + "), calling with " + (n-1));
printOneToN(n - 1);
System.out.println("Returned from previous call. n now equals " + n);
}
System.out.println(n);
System.out.println("printOneToN returning.");
return; // not needed - here for clarity
}
}```
Regards,
Jim

19. Member
Join Date
Nov 2013
Posts
9
Rep Power
0

## Re: Confused about the result of method

That really clears up a lot, thank you Jim!

20. ## Re: Confused about the result of method

That method recursively counts the number of occurrences of character x in an array a; the index i is the start index from where to count and r is the number of occurrences so far (they are just 'technicalities')

kind regards,

Jos

#### Posting Permissions

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