确定两个参与者的equals()方法不起作用

时间:2015-11-08 18:59:13

标签: java equals

我做错了什么?

问题是我将如何包含equals()方法,如果它们在所有三个字段中具有相同的值,则确定两个Participant是相等的? 包括为每个字段分配参数值的构造函数和返回包含所有值的String的toString()方法。

import javax.swing.JOptionPane;
import java.util.*;

public class Abc {

    private static Ab mini[] = new Ab[2];
    private static Ab diving[] = new Ab[2];

    public static void main(String[] args) {

        String name = "";
        String add = "";
        int age = 0;

        Ab p = new Ab(name, age, add);
        Ab p1 = new Ab(name, age, add);

        setParticipant();
        setParticipant1();

        displayDetail();
        displayDetail1();

        if (p == p1) {

            System.out.println("equal");
        } else {
            System.out.println("not equal");

        }

    }

    public static void setParticipant() {
        for (int x = 0; x < mini.length; x++) {
            System.out.println("Enter detail about participant1 " + (x + 1) + "...");

            String name = getName();
            String add = getAdd();
            int age = getAge();

            System.out.println();
            mini[x] = new Ab(name, age, add);
            //Create the object with the data you collected and put it into your array.

        }
    }

    public static void setParticipant1() {
        for (int y = 0; y < diving.length; y++) {
            System.out.println("Enter Participant2 " + (y + 1) + "...");

            String name = getName();
            String add = getAdd();
            int age = getAge();
            System.out.println();
            //mini[x] = new Ab(name, age, add);
            //Create the object with the data you collected and put it into your array.
            diving[y] = new Ab(name, age, add);
        }
    }

    public static void displayDetail() {
        // for (int y = 0; y < diving.length; y++) {
        System.out.println("Name \tAge \tAddress");

        for (int x = 0; x < mini.length; x++) {
            System.out.println(mini[x].toString());
            // System.out.println(diving[y].toString());
        }
    }

    public static void displayDetail1() {
        System.out.println("Name \tAge \tAddress");

        for (int y = 0; y < diving.length; y++) {
            System.out.println(diving[y].toString());
        }
    }

    public static String getName() {
        Scanner sc = new Scanner(System.in);
        String name;

        System.out.print(" Participant name: ");
        return name = sc.next();
    }

    // System.out.print(" Participant name: ");
    // name = sc.next();
    public static int getAge() {
        int age;
        System.out.print(" Enter age ");
        Scanner sc = new Scanner(System.in);
        return age = sc.nextInt();
    }

    public static String getAdd() {
        String add;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Address: ");
        return add = sc.next();
    }
}
public class Ab {

    private String name;
    private int age;
    private String address;

    public Ab(String strName, int intAge, String strAddress) {
        name = strName;
        age = intAge;
        address = strAddress;
    }

    @Override
    public String toString() {
        return name + " " + age + " " + address;
    }

    public boolean equals(Participant value) {
        boolean result;

        if (name.equals(name) && age == value.age && address.equals(address)) {
            result = true;
        } else {
            result = false;
        }

        return result;
    }
}

3 个答案:

答案 0 :(得分:6)

equals应覆盖Object的{​​{1}}方法,这意味着它应接受equals作为参数。

更改

Object

public boolean equals(Participant value)

哦,如果您不使用它,那么覆盖@Override public boolean equals(Object value) 也无济于事。

更改

equals

p==p1

答案 1 :(得分:0)

根据您提供的代码,我在任何地方都看不到参与者课程,这意味着public boolean equals(Participant value)无法开始使用。我建议你的equals方法看起来更像这样:

public boolean equals(Ab value) {
    if (this.name.equals(value.name) && this.age == value.age && this.address.equals(value.address)){
        return true;
    }
    else{
        return false;
    }
}

我还对您的Abc课程进行了一些修改以供您考虑。在我看来,它看起来比原始代码更清晰,更流畅,但同样也是你的选择:)

import javax.swing.JOptionPane;
import java.util.*;

public class Abc {
    private static Ab mini[] = new Ab[2];
    private static Ab diving[] = new Ab[2];

    public static void main(String[] args) {

        String name = "";
        String add = "";
        int age = 0;
        Ab p = new Ab(name, age, add);
        Ab p1 = new Ab(name, age, add);
        for(int x=0; x<mini.length; x++){
            System.out.println("Enter detail about participant " + (x + 1) + "...");
            setParticipant(mini[x]);
        }

        for (int y = 0; y < diving.length; y++) {
            System.out.println("Enter detail about participant " + (y + 1) + "...");
            setParticipant(diving[y]);
        }

        displayDetail(mini);
        displayDetail(diving);

        if (p.equals(p1)) {

            System.out.println("equal");
        } else {
            System.out.println("not equal");

        }

    }
    public static void setParticipant(Ab part) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Name: ");
        String name = sc.next();
        System.out.print("Age: ");
        int age = sc.nextInt();
        System.out.print("Address: ");
        String add = sc.next();
        System.out.println();
        part = new Ab(name, age, add); //<--- Create the object with the data you collected and put it into your array.
        sc.close();
    }

    public static void displayDetail(Ab[] parts) {

        System.out.println("Name \tAge \tAddress");

        for (int x = 0; x < parts.length; x++) {
            System.out.println(parts[x].toString());
        }

    }

}

答案 2 :(得分:-1)

尝试value.name.equals(name)和value.address.equals(address)因为你现在正在比较同样的东西