0

I'm trying to complete a simple program using multiple methods to compute the grade of a test, yet my method will not return any letters. The IDE says that my method must return a result of type String.

public static String getGrade1(int num1) {
    if (num1 <= 100 && num1 >= 90) {
        String a = "A";
        return a;
    } else if (num1 < 90 && num1 >= 80) {
        String b = "B";
        return b;
    }else if (num1 < 80 && num1 >= 70) {
        String c = "C";
        return c;
    }else if (num1 < 70 && num1 >= 60) {
        String d = "D";
        return d;
    }else if (num1 < 60) {
        String f = "F";
        return f;
    }
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
pchova
  • 99
  • 6

4 Answers4

1

Your functions must always return a value if it's not of the type void. The problem is that if you call getGrade1(110) your function wouldn't reach a return statement. Add an else clause at the end (without a trailing if) that returns something and it should stop giving you warnings. This code should work:

public static String getGrade1(int num1) {
    if (num1 <= 100 && num1 >= 90) {
        return "A";
    } else if (num1 < 90 && num1 >= 80) {
        return "B";
    } else if (num1 < 80 && num1 >= 70) {
        return "C";
    } else if (num1 < 70 && num1 >= 60) {
        return "D";
    } else if (num1 < 60) {
        return "F";
    } else {
        return "";
    }
}
Johan
  • 3,577
  • 1
  • 14
  • 28
1

To fix your problem, it is easiest to just add a default return statement to the end of the method, for example:

public String method() {
    // Code
    return ""; // Return some default String value
}

Note:

If you would rather have an exception occur instead of returning a default value, you could do something like the following:

public String method() throws Exception {
    // Code
    throw new Exception(); // Throw some exception
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • 1
    Personally I'd prefer to have the compiler tell me that I've missed something rather than returning a default value that might end up not being what I want. – Ivar Sep 23 '18 at 00:33
  • 1
    You can "return" by throwing an exception if the case should never have occurred. – Chai T. Rex Sep 23 '18 at 00:35
  • @LAD The problem is that when you always have some default return value, you can run in some nasty bugs. Your code appears to run just fine while it is corrupting your data. I'd rather see an exception so I can consciously do something with it. – Ivar Sep 23 '18 at 00:57
0

Try this:

 public static String getGrade1(int num1) {
    String grade = "";
  if (num1 <= 100 && num1 >= 90) {
        grade = "A";
    } else if (num1 < 90 && num1 >= 80) {
        grade = "B";
    }else if (num1 < 80 && num1 >= 70) {
        grade = "C";
    }else if (num1 < 70 && num1 >= 60) {
        grade = "D";
    }else if (num1 < 60) {
        grade = "F";
    }else{
        grade = "NA";
    }
     return grade;
}
Shekhar
  • 305
  • 1
  • 3
  • 14
-4

I believe if your return statement is in a if-else statement, then the compiler won't consider it as the return statement. Do what vonNikalasson did, but get rid of the else statement at the end and just return at the end.

public static String getGrade1(int num1) {
    if (num1 <= 100 && num1 >= 90) {
        return "A";
    } else if (num1 < 90 && num1 >= 80) {
        return "B";
    } else if (num1 < 80 && num1 >= 70) {
        return "C";
    } else if (num1 < 70 && num1 >= 60) {
        return "D";
    } else if (num1 < 60) {
        return "F";
    }
    return;
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
Hmm
  • 79
  • 6