?
Solved

Java return statement

Posted on 2013-11-28
8
Medium Priority
?
431 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 36

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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