-1

I am new at java and creating my first project which is a calculator using intellij. I have created two if else statements the first one takes the average given by the user and turns it into a grade. The second one is supposed to take the grade then turn it into a gpa. i have declared String grade; and String gpa.

  if (avg >= 90.0 ) {
    grade = "A";
  } else if (avg >=80.0) {
    grade = "B";
  } else if (avg >=70.0){
    grade = "C";
  } else if (avg >=60.0) {
    grade = "D";
  } else {
    grade = "F";
  }
  System.out.println("Grade is  " + grade );
  if (grade = A ) {      
    gpa = "4.0";        
  } else if (grade = B) { 
    gpa = "3.0";        
  }

Edit: The grade equals solution fixes the first error with the red underline but it doesn't compile. So I ended up using a switch statement instead in order to get two print statements.

  • The issue is the A next to grade = is highlighted red and I am not sure how to fix. Also the same thing happens for B. – new_coder01 Sep 22 '19 at 20:45
  • 2
    You seem to try to check for the string "A" but are missing the quotation marks. Also you have an assignment in the if-condition and not a comparison. Probably not what you want to do. – user10455554 Sep 22 '19 at 20:48
  • @user10455554 when i add quotation marks it underlines the entire line (grade = "A") in red – new_coder01 Sep 22 '19 at 21:12
  • 2
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – dnault Sep 22 '19 at 21:18
  • 1
    A single equals sign is the assignment operator. If you just want your code to compile, you need to change it to a double equals, like `if (grade == "A")`. But if you want your code to actually be *correct* you need to compare strings using the equals() method: `if (grade.equals("A"))` (see the possible duplicate question) – dnault Sep 22 '19 at 21:20
  • Try `if (grade.equals("A"))` (and same at the else if) See the linked question posted by user dnault – user10455554 Sep 22 '19 at 21:20
  • @dnault it got rid of the red underline from the A but it doesn't let me display the gpa with System.out.println(" gpa is " + gpa "); there is a red underline in gpa now and i can't check if it complies – new_coder01 Sep 22 '19 at 23:36

2 Answers2

1
if (avg >= 90.0 ) {
   grade = "A";
} else if (avg >=80.0) {
   grade = "B";
} else if (avg >=70.0) {
   grade = "C";
} else if (avg >=60.0) {
   grade = "D";
} else {
   grade = "F";
}
System.out.println("Grade is  " + grade );
if (grade.equals("A")) {      
   gpa = "4.0";        
} else if (grade.equals("B")) { 
   gpa = "3.0";        
}
Hidberg
  • 86
  • 5
  • That compiles, but is not [the correct way to compare strings](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – dnault Sep 22 '19 at 21:21
0

Several problems:

if (grade = A ) 

to check if it's equal, you should use == (for comparing references) or equals for string.

A is not a variable, but a string you want to compare to, so it should be "A"

there's no really point in doing it in two separate if's. do it once:

if (avg >= 90.0 ) {
   grade = "A";
   gpa = "4.0";
} else if (avg >=80.0) {
   grade = "B";
   gpa = "3.5";
} else if (avg >=70.0){
...
...
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
  • The reason I made separate if else statements is because I wanted the program to print a separate line telling the user what their grade is and what their gpa is. I am not sure if it can work combined. – new_coder01 Sep 22 '19 at 23:40
  • You can still have two prints at the end – Nir Levy Sep 23 '19 at 04:21