0

I have an array configured with students names, IDs, and grades, etc. I'm writing a method to pull the grades from the array, but I get this "boolean error" because I have a string. How do I correct it?

Here is the piece of code in question:

//code to print the students GPA based off the three grades received in  class
public static void print_average_grade(String myStudentID)
{
    for (Student variable : myStudent)
    {
        if (variable.getId() = (myStudentID)) //if statement is kicking back error....
        {
            double gpa = ((variable.getMyGradeA() + variable.getMyGradeB() + variable.getMyGradeC()) / 3);
            System.out.println("Student with ID# " + myStudentID + " has a GPA of " + gpa);
        }
Pang
  • 9,564
  • 146
  • 81
  • 122

2 Answers2

0

= denotes assignment. For comparison, use == (compare by reference) or .equals() (compare by value).

I.e., variable.getId() = (myStudentID) should probably be variable.getId().equals(myStudentID)

Roney Michael
  • 3,964
  • 5
  • 30
  • 45
-1

= is an assignment operator, which means it assigns the RHS to the LHS

foo=bar;   //the variable foo now contains the value bar

To check for equality use ==

foo==bar; //returns true or false 

However, with reference variable, == only checks if they refer to the same object, so for comparison of two Strings you should use

variable.getId.equals(myStudentId);  //checks if the values of the Strings is the same
Rohan Kamat
  • 239
  • 1
  • 6