-3

I am making a program for a grade calculator, as well as a GPA calculator. In my GPA calculator I have code to take the grade they entered for their course and it will display a letter grade based on what they entered when they hit calculate. I later then use that letter grade displayed for some math in the calculation. When I run the program, it keeps returning me 0, though the code to get the letter grade is pretty far up in my code and the other math is a method near the end.

if(Double.parseDouble(txt11.getText()) < 50){
                txt12.setText("F");
} else if(Double.parseDouble(txt11.getText()) >= 50 && Double.parseDouble(txt11.getText()) <= 59){
                    txt12.setText("D");
} else if(Double.parseDouble(txt11.getText()) >= 60 && Double.parseDouble(txt11.getText()) <= 64){
                    txt12.setText("C");
} else if(Double.parseDouble(txt11.getText()) >= 65 && Double.parseDouble(txt11.getText()) <= 69){
                    txt12.setText("C+");
} else if(Double.parseDouble(txt11.getText()) >= 70 && Double.parseDouble(txt11.getText()) <= 74){
                    txt12.setText("B");
} else if(Double.parseDouble(txt11.getText()) >= 75 && Double.parseDouble(txt11.getText()) <= 79){
    txt12.setText("B+");
} else if(Double.parseDouble(txt11.getText()) >= 80 &&           Double.parseDouble(txt11.getText()) <= 84){
                    txt12.setText("A");
} else if(Double.parseDouble(txt11.getText()) >= 85){
                    txt12.setText("A+");
}

and this is my method that keeps returning 0.

public double getGP(JTextField a){
    double b = 0.0;
    if(a.getText().equals("A")){
       b = 4.0;
    } else if(a.getText().equals("B+")){
       b = 3.5;
    } else if(a.getText().equals("B")){
       b = 3.0;
    } else if(a.getText().equals("C+")){
       b = 2.5;
    } else if(a.getText().equals("C")){
       b = 2.0;
    } else if(a.getText().equals("D")){
       b = 1.0;
    } else if(a.getText().equals("F")){
       b = 0.0;
    }
        return b;
}
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
Craig
  • 364
  • 1
  • 3
  • 25

1 Answers1

3

First, let's fix getGP(). Get the text once, then trim it and call toUpperCase() on it. Finally, you can eliminate the local double with something like,

public double getGP(JTextField a) {
    // Don't forget, this needs to be txt12 (not txt11).
    String str = a.getText();
    str = (str != null) ? str.trim().toUpperCase() : "";
    if (str.equals("A")) {
        return 4.0;
    } else if (str.equals("B+")) {
        return 3.5;
    } else if (str.equals("B")) {
        return 3.0;
    } else if (str.equals("C+")) {
        return 2.5;
    } else if (str.equals("C")) {
        return 2.0;
    } else if (str.equals("D")) {
        return 1.0;
    }
    return 0.0;
}

Then we can update your letter grade code, it can be simplified immensely by parsing the number once and an else will only evaluate if the preceding if was false (so you could use) -

Double val = Double.parseDouble(txt11.getText());
if (val < 50) {
    txt12.setText("F");
} else if (val <= 59) {
    txt12.setText("D");
} else if (val <= 64) {
    txt12.setText("C");
} else if (val <= 69) {
    txt12.setText("C+");
} else if (val <= 74) {
    txt12.setText("B");
} else if (val <= 79) {
    txt12.setText("B+");
} else if (val <= 84) {
    txt12.setText("A");
} else {
    txt12.setText("A+");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    Personally I'd use a map of grade/value if it's deterministic like this. – Dave Newton Aug 19 '14 at 13:22
  • I appreciate the code fixup, but it does not solve my issue, where getGP is always returning 0.0 even though the letter grades are being set way above where I am calling the method. – Craig Aug 19 '14 at 13:54
  • @Kalkrin Log the letter grade. It cannot be set as you think. – Elliott Frisch Aug 19 '14 at 14:13
  • 1
    @Kalkrin in the functoin getGP You omitted the case "A+" I suppose. – gaetanoM Aug 19 '14 at 14:29
  • @gaemaf that actually was the problem because when I was testing it I was just tossing in 85.. which is A+ so it was just returning the default 0.0. Stupid mistake. – Craig Aug 19 '14 at 22:44