类型对象中的equals(object)方法不适用于参数

时间:2014-08-04 00:49:40

标签: java class object methods equals

我有一个程序,我必须看看输入的两个名字是否相等,并获得有关名称的其他信息(例如长度,或获取首字母)。其他所有东西都运行正常,但是equals方法。

我收到错误: 类型对象中的equals(object)方法不适用于参数(Name,Name)

我无法弄清楚如何解决此错误。有人可以帮我理解错误的含义吗?

这是我的所有方法的类名:

class Name {

    private String firstName;
    private String middleName;
    private String lastName;

    public Name(String first, String middle, String last) {
        firstName = first;
        middleName = middle;
        lastName = last;
    }

    public String getFirst() {
        return firstName;
    }

    public String getMiddle() {
        return middleName;
    }

    public String getLast() {
        return lastName;
    }

    public String firstMiddleLast() {
        return (firstName + " " + middleName + " " + lastName);
    }

    public String lastFirstMiddle() {
        return (lastName + ", " + firstName + " " + middleName);
    }

    public boolean equals(Object name, Object otherName) {
        (name.toString()).toUpperCase();
        (otherName.toString()).toUpperCase();
        if (name.equals(otherName))
            return true;
        else
            return false; 
    }

    public String initials() {
        String initials = (firstName.toUpperCase().substring(0, 1) + middleName.toUpperCase().substring(0, 1) + lastName.toUpperCase().substring(0, 1));
        return initials;
    }

    public int length() {
        String wholeName = (firstName + middleName + lastName);
        return wholeName.length();
    }

}

这是测试名称的另一个类。

import java.util.Scanner;

public class TestName {

    public static void main(String[] args) {

        String first, middle, last;
        String firstOne, middleOne, lastOne;

        Scanner input = new Scanner(System.in);

        System.out.println("Name Program");
        System.out.println();
        System.out.println("Name of the first person...");
        System.out.println("Enter the first name: ");
        first = input.nextLine();
        System.out.println("Enter the middle name: ");
        middle = input.nextLine();
        System.out.println("Enter the last name: ");
        last = input.nextLine();


        Name name = new Name(first, middle, last);

        System.out.println("Name of the second person...");
        System.out.println("Enter the first name: ");
        firstOne = input.nextLine();
        System.out.println("Enter the middle name: ");
        middleOne = input.nextLine();
        System.out.println("Enter the last name: ");
        lastOne = input.nextLine();

        Name otherName = new Name(firstOne, middleOne, lastOne);

        System.out.println();
        System.out.println("Information about the first person: ");
        System.out.println("Full Name: " + name.firstMiddleLast());
        System.out.println("Last name first: " + name.lastFirstMiddle());
        System.out.println("Initials: " + name.initials());
        System.out.println("Name length: " + name.length());

        System.out.println();
        System.out.println("Information about the second person: ");
        System.out.println("Full Name: " + otherName.firstMiddleLast());
        System.out.println("Last name first: " + otherName.lastFirstMiddle());
        System.out.println("Initials: " + otherName.initials());
        System.out.println("Name length: " + otherName.length());

        if (equals(name, otherName))
            System.out.println("They are the same");
        else
            System.out.println("They are not the same");
    }
}

1 个答案:

答案 0 :(得分:0)

首先,需要修复此方法:

public boolean equals(Object name) 
{ //Now it is an override, so we use Object
    return firstMiddleLast().toUpperCase().equals(((Name)name).firstMiddleLast().toUpperCase());
}

您的版本使用(name.toString()).toUpperCase();,这有两个原因无意义。第一个是它创建一个新的String对象(因为字符串是不可变的)两次,使语句为零效果。其次,你在没有覆盖该方法的类上调用toString(),这样你就会得到一堆数字和字母。

最后,你需要改变这个:

if (equals(name, otherName))
        System.out.println("They are the same");
else
        System.out.println("They are not the same");

到此:

if (name.equals(otherName))
        System.out.println("They are the same");
else
        System.out.println("They are not the same");

由于在equals()中定义了Name方法,我们需要在Name对象上使用它。由于您没有尝试在名称对象上使用它,因此它尝试调用equals()中由Object定义的一个参数TestName方法,因此您的错误。