0

Hey I've recently started using Java, and I'm having trouble with a program I'm writing. Ive seen how the teacher has had his solution done but I dont see any difference in my solution compared to his except for me using a different equals in the if statements, and the names are different.

Can anyone tell me why I may be getting a 0.0 instead of a actual number grade?

* Grade Class *

public class Grade
{
private String grade;

public Grade(String getGrade)
{
    grade = getGrade;
}

public double getNumericGrade()
{
 double total = 0;
 double add = 0;

    String firstLetter = grade.substring(0, 1);
    if (grade.length() > 1)
    {
        String minusOrPlus = grade.substring(1, 2);
        if(minusOrPlus == "+")
            add = 0.3;
        if(minusOrPlus == "-")
            add = -0.3;
    }

    if (firstLetter == "A")
    {
        total = 4;
        if (add < 0) 
            total = total + add;
    }
    else if (firstLetter == "B")
        total = 3 + add;
    else if (firstLetter == "C")
        total = 2 + add;
    else if (firstLetter == "D")
        total = 1 + add;
    else if (firstLetter == "F")
        total = 0;

    return total;
}
}

Ive tested to see if my tester is wrong but if I put in the teachers code my tester works fine so I know it isnt that. But here it is anyway..

* GradeTester *

import java.util.Scanner;

public class GradeTester
{
public static void main (String[] args){
    Scanner in = new Scanner(System.in);


    System.out.print("Hey what letter grade did you get ? : ");
    String letterGrade = in.next();
    Grade x = new Grade(letterGrade);

    System.out.print("Your letter grade was " + letterGrade + " That means your number grade is " 
    + x.getNumericGrade());
    System.out.println("");



}
}

If you could help me out that would be awesome.

Thanks, Nathan

user2864740
  • 60,010
  • 15
  • 145
  • 220
NathanH
  • 17
  • 2

1 Answers1

0

All your string comparisons are failing:

firstLetter == "A" should be firstLetter.equals("A") and so on.

See Java comparison with == of two strings is false?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012