0

Why does this method return the error: Error: Unreachable code Thank you!

public static boolean zeroCheck(double numberTwo, String operator) {

    if (numberTwo == 0) {
        return false;
        if(operator == "/" || operator == "%")
            System.out.println("You cannot use a zero to divide or mod.");
        } else return true;
    }
}

Error:

File: C:\JAVA\LABS\LabSix.java [line: 228]

Error: Unreachable code

Community
  • 1
  • 1
  • the sentence return should go to the end. – David Hackro Mar 22 '16 at 18:23
  • 1
    Welcome to Stack Overflow! Be sure to search for possible duplicates of your questions before posting. I am voting to close this question because I agree with Brian above. – Matt C Mar 22 '16 at 18:29
  • Oh yes, that is a duplicate I will search more thoroughly next time. Please do close it. –  Mar 22 '16 at 18:53

3 Answers3

2

You return a value before you procceed to the rest of the code that is never reached. When you use the return statement, it automatically ends the code and returns boolean in your case.

Just put your return statement in the end of your block.

public static boolean zeroCheck(double numberTwo, String operator) {    
   if (numberTwo == 0) {      
      if (operator == "/" || operator == "%") {
         System.out.println("You cannot use a zero to divide or mod.");
      }
      return false;
   } else return true
}

By the way, if you want to compare String, please use equals(..) method, because String is not the primitive type like int or double etc. are. Generally, use equals(..) in case of comparing all objects.

if (operator.equals("/") || operator.equals("%"))
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
2

I'm assuming you meant to put the return false after the operator check. What happens is that the function returns when the numberTwo is 0 before the code gets a chance to check the if statement, making this the unreachable code

Farlan
  • 1,860
  • 2
  • 28
  • 37
1

Don't worry, this is a common misunderstanding for newcomers, it's not just you. :-) The return keyword does two things:

  1. Determines what the return value of the function will be, and

  2. Exits the function at that point

Newcomers sometimes think it just does the first, not the second. (And there are languages where the two are in fact done separately, Java just isn't one of them.)

So in your example, the unreachable code is the code after the return false;, since when the return false; statement is executed, it exits the function.

Just put it after the other code in that block:

public static boolean zeroCheck(double numberTwo, String operator)
{
    if (numberTwo == 0)
    {
        if (operator.equals("/") || operator.equals("%"))
        {
            System.out.println("You cannot use a zero to divide or mod.");
        }
        return false;
    }
    else // See note #3 below, you don't really need this
    {
        return true;
    }
}

A couple of other notes on the code above:

  1. You don't compare strings in Java with ==, you use str.equals(otherStr). More: How do I compare strings in Java?

  2. Note that I added braces around the inner block (the one attached to if (operator.equals...). They aren't strictly-speaking necessary, but when you're using braces in the outer block (which you have to), leaving them off the inner block can trip up someone editing the code later.

  3. Since your if block ends with return false;, there's no need for the else; you could just follow the end of the if block with return true; It can't be reached if you went into the block, because of course, you exited the function.

  4. The code above returns false if numberTwo is 0, even if the operator isn't / or %. That's what I think your original code meant to do, but I thought I'd flag it up.

Re #3 above, another option is to remember your return value in a variable:

public static boolean zeroCheck(double numberTwo, String operator)
{
    boolean numberTwoIsZero = numberTwo == 0;
    if (numberTwoIsZero)
    {
        if (operator.equals("/") || operator.equals("%"))
        {
            System.out.println("You cannot use a zero to divide or mod.");
        }
    }
    return numberTwoIsZero;
}
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    I see, thank you. Yes, I am a newcomer but great answers like these will help change that! –  Mar 22 '16 at 18:28
  • 1
    Oh wow the string comparison, I had no clue! *Face palm* I am forever in debt to you sir. –  Mar 22 '16 at 18:39