-1

' have this error despite there being many return statements returning a value depending on specific string inputs from the user.

public int convertPosition(String pos)
    {
        if (pos.equals("dean"))
        {
            return 0;
        }
        else if (pos.equals("chair"))
        {
            return 1;
        }
        else if (pos.equals("professor"))
        {
            return 2;
        }
        else if (pos.equals("secretary"))
        {
            return 3;
        }
        else if (pos.equals("teaching"))
        {
            return 4;
        }
        else if(pos.equals("otherwise"))
        {
            return -1;
        }
    }
J Smith
  • 1
  • 2
  • What do you believe the return value is if `pos` is `"FOO"`? And remember, even if you believe that it can never have that value, the compiler doesn't know that. Besides, you might be wrong, and at some point in the future you change the caller code to pass some new value. – Andreas Feb 13 '20 at 03:14

1 Answers1

0

This is a baaaad method.

    else if(pos.equals("otherwise"))
    {
        return -1;
    }
    return -999;
Nick
  • 805
  • 5
  • 14