为什么在使用getter时我不能比较两个字符串?

时间:2016-03-25 06:22:08

标签: java string methods compare

public class HighSchoolStudent {
    private String firstname;
    private String lastname;
    private double gpa;

    public HighSchoolStudent(String firstname, String lastname, double gpa) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.gpa = gpa;
    }

    public String getFirstName() {
        return firstname;
    }

    public String getLastName() {
        return lastname;
    }

    public double getGpa() {
        return gpa;
    }

    public String toString() {
        return (lastname + ", " + firstname);
    }
}



import java.util.*;

public class StudentSearchSort {
   public static void main(String[] args) {
      HighSchoolStudent Mike = new HighSchoolStudent("Michael", "Jackson", 3.5);
      HighSchoolStudent Roger = new HighSchoolStudent("Roger", "Federer", 3.9);
      HighSchoolStudent Serena = new HighSchoolStudent("Serena", "Williams", 3.7);
      HighSchoolStudent Kobe = new HighSchoolStudent("Kobe", "Bryant", 3.3);
      HighSchoolStudent Stephen = new HighSchoolStudent("Stephen", "Curry", 4.0);
      HighSchoolStudent Tiger = new HighSchoolStudent("Tiger", "Woods", 2.9);
      HighSchoolStudent Kanye = new HighSchoolStudent("Kanye", "West", 1.5);

      ArrayList<HighSchoolStudent> studentlist = new     ArrayList<HighSchoolStudent>();
  studentlist.add(Serena);
  studentlist.add(Tiger);
  studentlist.add(Mike);
  studentlist.add(Kanye);
  studentlist.add(Roger);
  studentlist.add(Stephen);
  studentlist.add(Kobe);

 // for (int i = 0; i < studentlist.size(); i++) {
 //    System.out.println(studentlist.get(i));  
 // }

//  System.out.println(highestGpa(studentlist));
  }

   public static String highestGpa(ArrayList<HighSchoolStudent> students) {
  HighSchoolStudent smartest = null;
  double highestgpa = 0.0;
  for (int i = 0; i < students.size(); i++) {
     if (students.get(i).getGpa() > highestgpa) {
        smartest = students.get(i);
        highestgpa = students.get(i).getGpa();
     }   
  }
  return smartest.toString() + " GPA: " + smartest.getGpa();
  }

   public static void lastNameSort(ArrayList<HighSchoolStudent> students) {
  HighSchoolStudent[] sortedlist = new HighSchoolStudent[students.size()];
  for (int i = 0; i < students.size(); i++) {
     for (int j = i; j < students.size(); j++) {
        int num = 10;
        if(num <= 0) {
           sortedlist[i] = students.get(i);
           sortedlist[i+1] = students.get(i+1);
        }else {
           sortedlist[i] = students.get(i+1);
           sortedlist[i+1] = students.get(i);
        }   
     }   
  }     
  }

 public static String lastNameSearch(ArrayList<HighSchoolStudent> students,     String lastname) {
  for(HighSchoolStudent student : students ) {
     if (lastname.equalsIgnoreCase(student.getLastName())) {
        HighSchoolStudent temp = students.get(i);
        return (i+1) + ": " + temp.toString();
     }
  }
  return null;
  }
  }

更新。这就是我拥有的一切。我不知道该怎么做了。现在我只是输入随机的东西所以它会让我编辑这些变化。我继续编译错误说错误:找不到符号和箭头指向之前的时期.getLastName(),我不知道如何修复它

3 个答案:

答案 0 :(得分:1)

有2个错误。

  1. 你弄错了。该参数名为students,而不是student。你错过了一个&#39;
  2. students是一个ArrayList,它没有getLastName()方法。您需要在getLastName()对象上调用HighSchoolStudent,如下所示:students.get(i).getLastName()
  3. 这就是你所拥有的:

    public static String lastNameSearch(ArrayList<HighSchoolStudent> students,     String lastname) {
        for (int i = 0; i < students.size(); i++) {
        //  if (lastname.equalsIgnoreCase(student.getLastName())) {
                //                        ^ this is where your error is
                //                          you missed an 's'...
                // vvv replace above with this line vvv
            if (lastname.equalsIgnoreCase(students.get(i).getLastName())) {
                HighSchoolStudent temp = students.get(i);
                return (i+1) + ": " + temp.toString();
            }
        }
        return null;
    }
    

答案 1 :(得分:0)

使用equalsIgnoreCase代替

 if (lastname.equalsIgnoreCase(students.get(i).getLastName())) {

您正在将整个对象转换为字符串而不是全名。

String temp = students.get(i).getFirstName()+" " + students.get(i).getLastName();

 return (i + 1) + ": " + temp;

答案 2 :(得分:-1)

public  static String lastNameSearch(List<HighSchoolStudent> `students`, String lastname) {
  for(HighSchoolStudent student:students) {
     if (lastname.equals(student.getLastName())) {
        HighSchoolStudent temp = student;
        return (i+1) + ": " + temp.toString();
     }
  }
  return null;