[英]Why won't my equals method work in this class? It's not functioning correctly
Basically I want it to stop and print a message if they've already added that person to the ArrayList, but it won't do that. 基本上我希望它停止并打印消息,如果他们已经将该人添加到ArrayList,但它不会这样做。
Here's the method: 这是方法:
@Override
public boolean equals(Object o) {
if (this.name == ((Student)o).getName() && this.ID == ((Student)o).getID()) {
return true;
}
else {
return false;
}
}
And the code section it's used in: 它的代码部分用于:
public void addStudents() {
Scanner keyboard = new Scanner(System.in);
String name = "", ID = "";
System.out.println("Welcome! Please type exit at any point to stop entering students and for the lottery to commence.\n");
System.out.print("Student name: ");
name = keyboard.nextLine();
if (!name.equals("exit")) {
System.out.print("Student ID: ");
ID = keyboard.nextLine();
}
while (!name.equals("exit") && !ID.equals("exit")) {
System.out.print("\nStudent name: ");
name = keyboard.nextLine();
if (!name.equals("exit")) {
System.out.print("Student ID: ");
ID = keyboard.nextLine();
if (!ID.equals("exit")) {
boolean contains = false;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).equals((new Student(name, ID)))) {
contains = true;
}
}
if (!contains) {
students.add(new Student(name, ID));
}
else {
System.out.println("You can only enter once.");
}
}
}
}
}
I've toiled over this for quite awhile, but can't put my finger on why it won't work. 我已经习惯了很长一段时间,但是我无法理解为什么它不起作用。
使用equals()方法比较字符串,而不是==
You should also be using String.equals
in your Student.equals
method: 您还应该在Student.equals
方法中使用String.equals
:
if (this.getName().equals(((Student) o).getName()) &&
this.getID().equals(((Student)o).getID()))
Your Student.equals
uses ==
for String
comparison, comparing object references, which will fail if the Strings
are lexicographically equal, but not the same String
object. 您的Student.equals
使用==
进行String
比较,比较对象引用,如果Strings
在字典上相等,则会失败,但不会是相同的String
对象。
There is problem in your equals
method.. 你的equals
方法有问题..
public boolean equals(Object o) {
if (this.name == ((Student)o).getName() && this.ID == ((Student)o).getID()) {
return true;
}
else {
return false;
}
}
You should compare name
using equals()
method in this also.. 你应该使用equals()
方法比较name
。
this.name.equals(((Student)o).getName())
And if your ID is also a String, do this for it also.. 如果你的ID也是一个字符串,也可以为它做这个..
Use equals
to compare names and ID's. 使用equals
来比较名称和ID。 In this case, ==
comparation boils down to an object comaration that will result in false since both names (each one a different String
) are different objects. 在这种情况下, ==
比较归结为一个将导致false的对象comaration,因为两个名称(每个名称都是不同的String
)是不同的对象。 It will not make a literal comparation. 它不会进行文字比较。
Just in case, since it's a name comparation you might want to use equalsIgnoreCase
because, for example, John Doe
is in essence the same name than john doe
. 以防万一,因为它是一个名称比较,你可能想使用equalsIgnoreCase
因为,例如, John Doe
本质上是与john doe
相同的名字。 Same goes for IDs if they're alphanumeric. ID也是如此,如果它们是字母数字的话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.