【问题标题】:the method equals(object) in the type object is not applicable to the arguments类型对象中的方法 equals(object) 不适用于参数
【发布时间】:2014-08-04 00:49:40
【问题描述】:

我有一个程序,我必须查看输入的两个名称是否相等,并获取有关名称的其他信息(例如长度,或获取首字母)。除了 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");
    }
}

【问题讨论】:

  • name.equals(otherName)
  • @Azar 我修复了这个问题,但是当我再次为 otherName 输入相同的名称时,它仍然告诉我它不相等
  • 我给你写了更详细的答案。
  • 意识到(name.toString()).toUpperCase();,而(otherName.toString()).toUpperCase(); 什么都不做……

标签: java class object methods equals


【解决方案1】:

首先,这个方法需要修复:

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 对象上使用它。由于您没有尝试在名称对象上使用它,它尝试调用ObjectTestName 中定义的一个参数equals() 方法,因此您的错误。

【讨论】:

  • 谢谢!我不知道这会是个问题。我根据您的建议进行了更改,但是即使我输入了两次相同的名称,它仍然说名称不一样。
  • @user3904967 很抱歉,现在应该可以了。我看到您是 StackOverflow 的新手。如果这回答了您的问题,我将不胜感激您单击它旁边的复选标记将其标记为“已接受”。
猜你喜欢
  • 2018-01-09
  • 2011-12-08
  • 2011-12-03
  • 1970-01-01
  • 2012-06-24
  • 2014-06-26
相关资源
最近更新 更多