0
  public static boolean isValidDate(int month, int day) {
    if (month >= 3 && month <= 5) {
      if (month == 3) {
        if (day >= 1 && day <= 31) {
          return true;
        } else {
          return false;
        }
      } else if (month == 4) {
        if (day >= 1 && day <= 30) {
          return true;
        } else {
          return false;
        }
      } else if (month == 5) {
        if (day >= 1 && day <= 15) {
          return true;
        } else {
          return false;
        }
      }
    } else {
      return false;
    }
  }

Get these errors:not sure how to fix them im returning everything.

BoxOffice.java:81: error: missing return statement
    }
BoxOffice.java:85: error: missing return statement
    }
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • The error is straightforward: your method is missing a `return` statement. – Nir Alfasi Mar 02 '17 at 22:06
  • 3
    Possible duplicate of ["Missing return statement" within if / for / while](http://stackoverflow.com/questions/23058029/missing-return-statement-within-if-for-while) – Nir Alfasi Mar 02 '17 at 22:08
  • It is considered "bad" coding to directly return, maybe try using a boolean? – Josh Heaps Mar 02 '17 at 22:10
  • 2
    @JoshHeaps Says who? – shmosel Mar 02 '17 at 22:10
  • @JoshHeaps I assume you are referring to [single return points](http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement)? Sometimes it is appropriate, sometimes it is not. It's definitely bad (or, at least, unnecessary) programming to do `if (condition) { return true; } else { return false; }` (or `if (condition) { b = true; } else { b = false; }`), because it's just easier to write `return condition;` or `b = condition;`. I'd worry much more about that than multiple returns. – Andy Turner Mar 02 '17 at 22:13
  • Yes, I agree. The best way would be more along the lines of `b = false; if(condition) b = true; return b;` – Josh Heaps Mar 02 '17 at 22:16
  • @JoshHeaps no, don't do that. The initial assignment of `b` breaks definite assignment; and it's just more verbose. `b = condition;` is easier and safer. – Andy Turner Mar 02 '17 at 22:18
  • Oh yeah! I'm still learning the old ropes myself. I forgot about setting straight to a condition. – Josh Heaps Mar 02 '17 at 22:20

4 Answers4

3

The compiler isn't smart enough to deduce that the inner if covers every scenario in the range of the outer if. Just change

else if(month == 5) {

to

else { // month must be 5 here
shmosel
  • 49,289
  • 6
  • 73
  • 138
1

shmosel's answer describes the problem and the least disruptive fix.

Personally, I'd write this as a switch, and avoid writing the long if/else statements to check the day:

switch (month) {
  case 3:
    return (day >= 1 && day <= 31);
  case 4:
    return (day >= 1 && day <= 30);
  case 5:
    return (day >= 1 && day <= 15);
  default:
    return false;
}
Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

you need to transforme the else if (month == 5) to else ....

public static boolean isValidDate(int month, int day) 
     {
        if (month >= 3 && month <= 5) 
        {
          if (month == 3) 
          {
            if (day >= 1 && day <= 31) 
            {
              return true;
            } 
            else 
            {
              return false;
            }
          } 
          else if (month == 4)
          {
            if (day >= 1 && day <= 30) 
            {
              return true;
            } 
            else 
            {
              return false;
            }
          } 
          else  
          {
            if (day >= 1 && day <= 15) 
            {
              return true;
            } 
            else 
            {
              return false;
            }
          }
        } 
        else 
        {
          return false;
        }
      }
Mouaici_Med
  • 390
  • 2
  • 19
-1

Try making a boolean, and instead of returning in the if statement, change the boolean and return at the end of the method. Here is an example with your code.

public static boolean isValidDate (int month, int day) {
    boolean result = true; //This must be set to a value to avoid compiler errors
    if(month >= 3 && month <= 5) {
        if(month == 3) {
            if(day >= 1 && day <= 31) {
                result = true;
            }
            else {
                result = false;
            }
        }
        else if(month == 4) {
            if(day >= 1 && day <= 30) {
                result = true;
            }
            else {
                result = false;
            }
        }
        else if(month == 5) {
            if(day >= 1 && day <= 15) {
                result = true;
            }
            else {
                result = false;
            }
        }
    }
    else {
        result = false;
    }
    return result;
}

Hope that helps!

Josh Heaps
  • 335
  • 1
  • 8
  • 2
    Or just remove the `else` and `return false;` – shmosel Mar 02 '17 at 22:09
  • But then you are using a direct return. My professor taught the class to never do so. – Josh Heaps Mar 02 '17 at 22:12
  • 1
    Ah, the old Professor vs. Programmer feud. – shmosel Mar 02 '17 at 22:13
  • See @AndyTurner's answer for an example where an early return is far preferable to a single return. – shmosel Mar 02 '17 at 22:17
  • 2
    This isn't really fixing the problem - and by setting the result arbitrarily, you risk the compiler not catching the logical case that you might have missed, because the variable is now definitely assigned. By setting it to *`true`*, you risk saying that something which should be invalid is actually not valid (generally the less safe choice). It is better to actually work out where the return value is missing (/the variable would not otherwise be assigned), and fix that. Compiler errors are there to help you. – Andy Turner Mar 02 '17 at 22:27