Link to home
Start Free TrialLog in
Avatar of Kyle Hamilton
Kyle HamiltonFlag for United States of America

asked on

Java return statement

I have this recursive function, which I would like to break out of based on the condition at the top of the function: if marker >= target, get out of the function.

I was under the impression that a return statement would break me out of the function. But that is not the case. It reaches the conditional, prints out "it's too big - oh no!!!!!", and proceeds to some code down below. What gives?

private boolean eatString(ListGraph dfa, String target, int start) throws IOException{
		if(marker >= target.length()){
			System.out.println("it's too big - oh no!!!!!");
			return false;
		};
		BufferedReader br = stringToBR(target.substring(start));
		int c; 
		int srcV = 0;
		int destV;

		while ((c = br.read()) != -1) {
			marker++;
			char targetChar = (char)c;
			Iterator<Edge> it = dfa.edgeIterator(srcV);
			while(it.hasNext()){
				Edge next = it.next();
				if(next.contains(targetChar)){
					System.out.println("woohoo! a match!: "+next);
					srcV = next.getDest();
					break;
				}else{
					System.out.println("oh, how sad: "+next+" marker: "+marker);
					eatString(dfa, target, marker);
				}
			}
		}
		return false;
	}

Open in new window

sample output:
woohoo! a match!: , [(0, 1): c]
woohoo! a match!: , [(1, 2): a]
oh, how sad: , [(2, 3): t] marker: 3
woohoo! a match!: , [(0, 1): c]
woohoo! a match!: , [(1, 2): a]
woohoo! a match!: , [(2, 3): t]
oh, how sad: , [(2, 3): t] marker: 7
it's too big - oh no!!!!!
oh, how sad: , [(2, 3): t] marker: 8
it's too big - oh no!!!!!
woohoo! a match!: , [(2, 3): t]

Open in new window

Avatar of chaau
chaau
Flag of Australia image

Just wondering: why does your function always return false? does it mean anything?
I would use the return value of the function to break the while loop, like this:
private boolean eatString(ListGraph dfa, String target, int start) throws IOException{
		if(marker >= target.length()){
			System.out.println("it's too big - oh no!!!!!");
			return false;
		};
		BufferedReader br = stringToBR(target.substring(start));
		int c; 
		int srcV = 0;
		int destV;

		while ((c = br.read()) != -1) {
			marker++;
			char targetChar = (char)c;
			Iterator<Edge> it = dfa.edgeIterator(srcV);
			while(it.hasNext()){
				Edge next = it.next();
				if(next.contains(targetChar)){
					System.out.println("woohoo! a match!: "+next);
					srcV = next.getDest();
					break;
				}else{
					System.out.println("oh, how sad: "+next+" marker: "+marker);
					if(!eatString(dfa, target, marker)) break; // this means: it's too big - oh no!!!!!
				}
			}
		}
		return true; // modify to true here, which means that target.length is still within marker
	}

Open in new window

Avatar of Kyle Hamilton

ASKER

well..., the reason it's returning false all the time, is that it is very much a work in progress.

What's disturbing me is I don't understand why after it encounters a return statement, it still proceeds to do more stuff...

I tried your version, and it still continues to execute after hitting "it's too big - oh no!!!!!"

why is that? I don't want to just exit one loop, I want to exit the whole function.

here's the output from your suggestion:

woohoo! a match!: , [(0, 1): c]
oh, how sad: , [(1, 2): a] marker: 2
oh, how sad: , [(0, 1): c] marker: 3
woohoo! a match!: , [(0, 1): c]
woohoo! a match!: , [(1, 2): a]
woohoo! a match!: , [(2, 3): t]
woohoo! a match!: , [(0, 1): c]
woohoo! a match!: , [(1, 2): a]
woohoo! a match!: , [(2, 3): t]
oh, how sad: , [(1, 2): a] marker: 10
it's too big - oh no!!!!!
oh, how sad: , [(1, 2): a] marker: 11
it's too big - oh no!!!!!
woohoo! a match!: , [(1, 2): a]
woohoo! a match!: , [(2, 3): t]

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that works. why wasn't it enough to return false at the top of the function like I had it? would you mind explaining?

Thanks.
this is great. thank you. If you could also explain it to me, I'd be super grateful.

cheers.
why wasn't it enough to return false at the top of the function like I had it?
Because that is the nature of a "recursive" algorithm. Say, at some point, you had recursively called eatString 4 times, ie. eatString has called eatString which has called eatString which has called eatString, then when you do "return false" all you are doing is returning from the innermost recursive call to eatString. The 3rd recursive call of eatString continues running, ie. it returns from the call on line 23 and continues to run line 24 (the end of the if block) which means that it will then continue through the inner loop, etc, etc.

Does that make it clearer?
Sure. It will require a drawing. I found this small picture in the internet: User generated imageHave a look at how the calls are performed. See, that there is a function() call and then the "logic"
If you break out at the second call the function will not call itself third and fourth time. However, it will continue to execute the "logic" part of the first call.
Now, if you break at the third call, the function's fourth call will not be executed, but the "logic" part of the first two calls will continue to execute.
By introducing the "false" return statement we have an option to skip this remaining logic.
In your case the "logic" part was a simple closing bracket of the while loop. However, as there were two of them, we have broke from the inner one, but the outer while continue to execute for all the remaining calls on stack
Thanks guys. I have so much to learn :)