Solved

Java return statement

Posted on 2013-11-28
8
423 Views
Last Modified: 2013-11-28
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
Comment
Question by:Kyle Hamilton
  • 4
  • 3
8 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39684335
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
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39684380
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
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39684387
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
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39684441
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 39684454
this is great. thank you. If you could also explain it to me, I'd be super grateful.

cheers.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39684463
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
 
LVL 24

Expert Comment

by:chaau
ID: 39684477
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
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39684616
Thanks guys. I have so much to learn :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now