【发布时间】: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