0

I don't understand why I am getting an error stating that the method getClassification must return a result of type char.

private char A, B, C, D;

public char getClassification() {
    if(parts[0] >= 0 && parts[0] <= 127)
        return (A);
    else if(parts[0] >= 128 && parts[0] <= 191)
        return (B);
    else if(parts[0] >= 192 && parts[0] <= 223)
        return (C);
    else if(parts[0] >= 224 && parts[0] <= 255)
        return (D);
}
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
S.Strachan
  • 49
  • 4
  • 1
    It's because all of your return statements are conditional. If all conditions are false, method will reach the end without returning anything. – Arjan Sep 10 '16 at 04:33
  • You need either, as stated in answers and comments, a 'default' `return` statement **or** if that `case` should never occur in your programm, you should `throw` an `[Runtime]Exception`. See [Fail Fast principle](http://principles-wiki.net/principles:fail_fast). – Markus Mitterauer Sep 10 '16 at 05:44

4 Answers4

3

The getClassification doesn't return a value when all the if condition is false.

private char A, B, C, D;

public char getClassification() {
    if(parts[0] >= 0 && parts[0] <= 127)
        return (A);
    else if(parts[0] >= 128 && parts[0] <= 191)
        return (B);
    else if(parts[0] >= 192 && parts[0] <= 223)
        return (C);
    else if(parts[0] >= 224 && parts[0] <= 255)
        return (D);
    else
       return //Something Else(char Value)
}

This should Work.

Kumar_Vikas
  • 837
  • 7
  • 16
  • Doesn't matter, even if you put `true` in each `if` statement (as `if(true)`), the compiler will complain. So the explanation is not 100% correct. – Saurav Sahu Sep 10 '16 at 04:45
  • Instead of the default `return` statement, a `throw new SomeException()` should be considered, if that's an _illegal state_ in that part of the code. // BTW, the `else` helps for verbosity but could also be omitted. – Markus Mitterauer Sep 10 '16 at 05:46
1

Need an else statement or default return statement

Kandera
  • 9
  • 5
0

You have put every return statement inside an if statement.

The compiler is not smart enough to figure out the surety of what you are returning from the function when you put all the return statement inside if condition (even if the function is definitely going to return the mentioned type).

Try compiling it, the compiler will complain the same thing:

public char getClassification() {
    if(true) return 'a';
}
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
0

You are returning character a,b,c,d depending on if condition but in some conditions if all conditions are false (e.g parts[0] >255 or parts[0] <0) then function returns nothing.To avoid the error write else condition at the end.

private char A, B, C, D;

public char getClassification() {
    if(parts[0] >= 0 && parts[0] <= 127)
        return (A);
    else if(parts[0] >= 128 && parts[0] <= 191)
        return (B);
    else if(parts[0] >= 192 && parts[0] <= 223)
        return (C);
    else if(parts[0] >= 224 && parts[0] <= 255)
        return (D);
    else
        return 'E';
}

private char A, B, C, D;

public char getClassification() {

if(parts[0] >= 0 && parts[0] <= 127) return (A);

else if(parts[0] >= 128 && parts[0] <= 191)
    return (B);
else if(parts[0] >= 192 && parts[0] <= 223)
    return (C);
else if(parts[0] >= 224 && parts[0] <= 255)
    return (D);

}