how can I compare between two variables in different objects with the same datatype?

Viewed 52
          import java.util.Scanner;
public class test{
  public static void main(String[]args){
    Scanner input=new Scanner(System.in);
       SE220Std [] stdDB=new SE220Std[2];
      for(int i=0;i<stdDB.length;i++){
        System.out.println("enter student's "+(i+1)+" name,id and gpa respectivly");
            String n=input.next();
    int d=input.nextInt();
    double g=input.nextDouble();
    stdDB[i]=new SE220Std (n,d,g);
    
    
  }
       for(int i=0;i<stdDB.length;i++){
      System.out.println("student "+(i+1)+" info:- \n"+stdDB[i].getName()+"\n"+stdDB[i].getID()+"\n"+stdDB[i].getGPA());
      System.out.println("-----------------");
       }
        for(int i=0;i<stdDB.length;i++){
          if(stdDB[0].getGPA()<stdDB[i].getGPA()){
            stdDB[0].getGPA()=stdDB[i].getGPA();
          }
          System.out.println(stdDB[0]);
        }
}
}
  
 class SEstd{
  private String name;
  private int id;
  private double gpa;
  
   SEstd () {
     name=null;
    id=0;
    gpa=0;
  }
   public SEstd(String newName,int newID,double newGPA){
     name=newName;
     id=newID;
     gpa=newGPA;
   }
public String getName() {
    return name;
  }

  public void setName(String newName) {
    this.name = newName;
  }
  public int getID() {
    return id;
  }

  public void setID(int newID) {
    this.id = newID;
  }
    public double getGPA() {
    return gpa;
  }

  public void setGPA(double newGPA) {
    this.gpa = newGPA;
  }
}

error: The left-hand side of an assignment must be a variable so I am trying to create a data base for students that saves their name ,id number and gpa in one object then save each object in an array. I wanted to add feature which is displaying the highest gpa but I had an error while testing and I didn't know how to change my code how can I compare the gpa above from each student to save the highest one ? I tried using multiple ways but nothing seemed right

1 Answers

You can't assign to the return of a getter like that. That's what setters are for:

stdDB[0].setGPA(stdDB[i].getGPA());
Related