Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement returning null

I'm trying to implement a grading system for a test using an enum and a switch statement, however with the current code I have the result is always "null". I can't see where iv'e gone wrong, can anyone help?

public enum Grade {
    A, B, C, D, E, U; 
}
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the students mark:");
    int points = scan.nextInt();

    if (points < 0 || points > 200) {
        System.out.println("Error! points must be between 0 & 200");
    } else {
       System.out.println(findGrade(points));
    }

}

public static Grade findGrade(int points) {
    switch (points) {
        case 1:
            if (points>= 0 && points <= 59) {
                return Grade.valueOf("U");
            }
        case 2:
            if (points >= 60 && points <= 89) {
                return Grade.valueOf("E");
            }
        case 3:
            if (points >= 90 && points <= 119) {
                return Grade.valueOf("D");
            }
        case 4:
            if (points >= 110 && points <= 139) {
                return Grade.valueOf("C");
            }
        case 5:
            if (points >= 140 && points <= 169) {
                return Grade.valueOf("B");
            }
            case 6:
            if (points >= 170 && points <= 200) {
                return Grade.valueOf("A");
            }
        default:
            return null;
    }
}

}
like image 801
Thaitan Avatar asked Feb 06 '26 21:02

Thaitan


2 Answers

Let's look at

switch (points) {
   case 1:
      if (points >= 0 && points <= 59) {
          return Grade.valueOf("U");
      }

What you're basically saying is:

if (points == 1) {
    if (points >= 0 && points <= 59) {
        return Grade.valueOf("U");
    }
}

which is nonsense. I don't think you need switch at all in this case. Just use:

if (points < 0) {
    return null;
}
if (points <= 59) {
    return Grade.valueOf("U");
}
if (points <= 89) {
    return Grade.valueOf("E");
}
if (points <= 119)
    return Grade.valueOf("D");
}
...
return null;
like image 126
Zong Avatar answered Feb 09 '26 10:02

Zong


You do not need a switch at all. Simply put your ifs in a chain, and that would work for you:

if (points>= 0 && points <= 59) {
    return Grade.valueOf("U");
}
if (points >= 60 && points <= 89) {
    return Grade.valueOf("E");
}
if (points >= 90 && points <= 119) {
    return Grade.valueOf("D");
}
if (points >= 110 && points <= 139) {
    return Grade.valueOf("C");
}
if (points >= 140 && points <= 169) {
    return Grade.valueOf("B");
}
if (points >= 170 && points <= 200) {
    return Grade.valueOf("A");
}
return null;

The idea behind the switch statement is to let you base a decision on a set of fixed values or small value ranges. In this case, however, the range is pretty large, so a chain of if statements looks like a more appropriate choice.

Considering the task that you are implementing, you could build a shorter solution with a lookup table and a loop:

int[] limits = new int[] {59, 89, 119, 139, 169, 200};
String[] grades = new String[] {"U", "E", "D", "C", "B", "A"};
for (int i = 0 ; i != limits.length ; i++)
    if (points <= limits[i])
         return grades[i];
return null;
like image 22
Sergey Kalinichenko Avatar answered Feb 09 '26 11:02

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!