0

I have a recursion call which is showing weird behavior.

    public String recursionMethod() {
    String repeatRun = null;
    repeatRun = scanner.next();
    if (!("Y".equals(repeatRun) || "N".equals(repeatRun))) {
        System.out.println("Please enter the correct value. (Y/N)");
        this.recursionMethod();
    }
    System.out.println("value of the entered variable->"+repeatRun);
    return repeatRun;
}

When the method run for the first time, I enter value "no". As expected, it enters the if block and so it calls itself again asking to enter either "Y" or "N". This time, I enter "Y". It does not enter the if block again but the log prints like this.

Do you want to enter another directory? (Y/N)
no
Please enter the correct value. (Y/N)
Y
value of the entered variable->Y
value of the entered variable->no

This behavior is strange. Why is it picking the old value again? On running in debug mode it shows that after the control goes to the return line, it again goes to the line "this.recursionMethod()" which is inside the if block.

Please help me understand and also how to fix this so that my method does not return the previous value.

3
  • You don't return the result of the recursive call. You just discard its result and then return the original, incorrect value. Commented Aug 25, 2016 at 18:22
  • I think you may be failing to understand how recursion works... the return keyword only goes up one layer, to the place where the current execution of this method was called from. Commented Aug 25, 2016 at 18:30
  • Yes right. But adding a return inside the if block solves the problem. Commented Aug 25, 2016 at 18:33

2 Answers 2

1

Try this :

    public String recursionMethod() {
        String repeatRun = null;
        repeatRun = scanner.next();
        if (!("Y".equals(repeatRun) || "N".equals(repeatRun))) {
            System.out.println("Please enter the correct value. (Y/N)");
            return this.recursionMethod();
        }
        System.out.println("value of the entered variable->"+repeatRun);
        return repeatRun;
    }

You forgot the return in the if block where you make the recursive call.

Sign up to request clarification or add additional context in comments.

Comments

0

When you check if the typed value is Y or N, if the value is wrong, you ask the user to type again a new value, making a recursive call. However, when this recurisve call ends, the method continues till the end, printing the value typed first. The right implementation should be this:

if (!("Y".equals(repeatRun) || "N".equals(repeatRun))) {
    System.out.println("Please enter the correct value. (Y/N)");
    repeatRun = this.recursionMethod();
}
else {
    System.out.println("value of the entered variable->"+repeatRun);
}

return repeatRun;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.