-2

I'm getting an unreachable statement error with this boolean declaration. I know unreachable usually means meaningless, but I need the isValid statement for my while loop to work. Why am I getting this error and how can I fix it? Here is my code.

I'm getting the error on boolean isValid;

Thank you in advance, for any input you might have.

public static double calculateMonthlyPayment(double loanAmount, double monthlyInterestRate, int months)
        {
            double monthlyPayment =
            loanAmount * monthlyInterestRate/
            (1 - 1/Math.pow(1 + monthlyInterestRate, months));
            return monthlyPayment;
            boolean isValid;
                      isValid = false;

            //while loop to continue when input is invalid
            while (isValid ==false)
            {
                System.out.print("Continue? y/n: ");
                                String entry;
                entry = sc.next();
                if (!entry.equalsIgnoreCase("y") && !entry.equalsIgnoreCase("n"))
                {
                    System.out.println("Error! Entry must be 'y' or 'n'. Try again.\n");
                }
                else
                {
                    isValid = true;
                } // end if

                sc.nextLine();

            } // end while
                        double entry = 0;
        return entry;


        }
tonno5309
  • 23
  • 4

6 Answers6

1

Yes, you have a return on the previous line. The method is done.

return monthlyPayment; // <-- the method is finished.
boolean isValid; // <-- no, you can't do this (the method finished on the
                 //     previous line).
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

You can't execute any code after the return statement. Once return is executed, the method will finish.

return monthlyPayment;
//this and the rest of the code below will never be executed
boolean isValid;
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

Because of your line return monthlyPayment; after return statement extra code in this scope will be unreachable ......as the return statement must be last last statement of that method Scope

user3272090
  • 126
  • 5
1

The Method is finished on your first return statement.

Either you can put this in some condition. so that there would be a possibility to go further

Nishant Modi
  • 669
  • 1
  • 6
  • 19
1

return monthlyPayment; statement is causing the issue. When you say return it means you are telling the control to return. No more execution.

Unreachable doesn't mean meaningless- its means that some piece of code is never going to be executed no matter what and that's what the compiler is trying to tell you by throwing an error.

So, you can either remove the unreachable code block if you don't need it or modify your method to return properly or conditionally.

For example -

//even if you use the below statement in your code
//compiler will throw unreachable code exception
return monthlyPayment;;
Tirath
  • 2,294
  • 18
  • 27
1

After returning the method the line after return statement will not reachable compiler always assumes that return is the end point of execution of any type of block of code or method

santhosh
  • 484
  • 3
  • 10