[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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

0
Kyle Hamilton
Asked:
Kyle Hamilton
  • 4
  • 3
1 Solution
 
chaauCommented:
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

0
 
Kyle HamiltonData ScientistAuthor Commented:
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

0
 
chaauCommented:
Sorry, I missed that you actually have two while loops. In this case you will either need to introduce a new variable, or just simple change the line to:
if(!eatString(dfa, target, marker)) return false; // this means: it's too big - oh no!!!!!

Open in new window

The whole code:
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)) return false; // 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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Kyle HamiltonData ScientistAuthor Commented:
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.
0
 
Kyle HamiltonData ScientistAuthor Commented:
this is great. thank you. If you could also explain it to me, I'd be super grateful.

cheers.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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?
0
 
chaauCommented:
Sure. It will require a drawing. I found this small picture in the internet: recursiveHave 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
0
 
Kyle HamiltonData ScientistAuthor Commented:
Thanks guys. I have so much to learn :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now