S.Strachan S.Strachan - 2 months ago 7
Java Question

Issue with returning a char

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);
}

Answer

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.

Comments