Solved

Java return statement

Posted on 2013-11-28
8
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 25

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 25

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 25

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 38
web application structure 18 115
Android development question 2 81
Chrome and Firefox Java 5 68
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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