-1

When working with recursion I realized I'm not sure how the return statement works. Does it stop and return true when target.contains(key) returns true or does it fall out and return false, because of the line below? Does the previous iterations of the method get finished so that it instead return false?

The program creates passwords and this method is called to check that the password contains one of the required fields, such as upper case letters, symbols or numbers. It's called with 4 separate sources and they are then used to tell the program to keep the password or to create a new one if it doesn't meet the required standards. I've done this program for fun to refresh my memory of Java, it's not a real program that anyone will ever use.

private static boolean containsKeyword(String target, String source, int placement){
    String key = String.valueOf(source.charAt(placement));
    if(target.contains(key))
        return true;

    if(placement==0)
        return false;
    containsKeyword(target, source, placement-1);
    return false;
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Alvar
  • 252
  • 1
  • 4
  • 13
  • It works exactly the same as in a non-recursive scenario. – Oliver Charlesworth Oct 11 '14 at 19:01
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – azurefrog Oct 11 '14 at 19:03
  • `a` and `b` are strings, and you're using the comparison operator on them. – Oliver Charlesworth Oct 11 '14 at 19:05
  • In your example, the comparison *is* relevant, because it yields the exact same result no matter how deep into the recursion you go. The function you're computing isn't a proper candidate for a recursive treatment. You need an example that breaks a and/or b down into simpler cases for this example to make any sense. –  Oct 11 '14 at 19:10
  • I've updated the question with the actual code now that it apparently is relevant... – Alvar Oct 11 '14 at 20:15
  • Recursion is a hard concept for many people to get their head around. You probably need to "execute on paper" -- walk through your code with an example, writing down what happens with each `if` statement, each call, each return. – Hot Licks Oct 11 '14 at 20:46

2 Answers2

7

You seem to be missing the whole point of the recursion step.

Change this:

someFunc(a, b, nbr-1);
return false; 

To this:

return someFunc(a, b, nbr-1);

By the way, recursively calling this function with the exact same data (the strings a and b) is pointless.

There must be something else that you want to call this function with (perhaps sub-strings of a and b).

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • @Alvar: Well, I can only answer what you post... – barak manos Oct 11 '14 at 19:09
  • @Alvar: OK, then the first part of my answer explains that issue... You're welcome :) – barak manos Oct 11 '14 at 19:10
  • 1
    @Alvar: BTW, if you keep changing your question, this answer may become irrelevant... – barak manos Oct 11 '14 at 19:11
  • @Alvar: Well you've just fixed the question according to the answer or answers given here, essentially making them obsolete and irrelevant. The whole purpose of this forum is to provide questions with appropriate corresponding answers, not for people to ask questions and then correct them into answers. Now nobody knows what the original question was, hence nobody can search for this question in the future and make use of it!!!!! – barak manos Oct 11 '14 at 20:21
  • I'm not going to argue with you, but I would guess that people have down-voted your question because you've changed it significantly several times. – barak manos Oct 11 '14 at 20:26
1

Your method will always return false if it doesn't get into the first if. You need to change this:

someFunc(a, b, nbr-1);
return false; 

to

return someFunc(a, b, nbr-1);

Maybe if you update your question with what exactly you are trying to do, you will get a more targetted answer that will help you understand the recursion better.

gkrls
  • 2,618
  • 2
  • 15
  • 29
  • it will always get into the second if when it's finished, which is the point of the second ig, manos solved it faster.. but thanks. – Alvar Oct 11 '14 at 19:16