-3

好吧,另一个问题已经很老了,我认为不太可能会有更多的人偶然发现它并阅读/回答它,所以这里是:鉴于标题:

public boolean equals(Name otherName)

我应该比较两个名称对象是否相等。我现在拥有的是:

public boolean equals(Name otherName){
        return (otherName.equalsIgnoreCase(this));
    }

正如上一个帖子中有人建议的那样。

但是,我收到一个找不到符号错误。

这是我要挑剔的其余代码。请原谅我的草率之类的,今晚我的代码写得不好。

名称.java:

public 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 + ", " + middleName + firstName);      
    }

    public boolean equals(Name otherName){
        return (otherName.equalsIgnoreCase(this));
    }

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

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

测试名称.java:

import java.util.Scanner;

public class TestNames{
    public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the first name, middle name, and last name in that order for the first name object.");
    Name name = new Name(scan.next(), scan.next(), scan.next());
    System.out.println("Enter the second name, middle name, and last name in that order for the first name object.");
    Name name2 = new Name(scan.next(), scan.next(), scan.next());   

    System.out.println("Printing name in the order first name, middle name, last name");
    System.out.println(name.firstMiddleLast());
    System.out.println(name2.firstMiddleLast());

    System.out.println("Printing name in the order last name, first name, middle name");
    System.out.println(name.lastFirstMiddle());
    System.out.println(name2.lastFirstMiddle());

    System.out.println("Printing initials");
    System.out.println(name.initials());
    System.out.println(name2.initials());

    System.out.println("Printing name length");
    System.out.println(name.length());
    System.out.println(name2.length());

    System.out.println("Are these names equal?");
    System.out.println(name.equals(name2));



    }
}
4

2 回答 2

0

您在使用时没有equalsIgnoreCase在类中实现方法Name

public boolean equals(Name otherName){
    return (otherName.equalsIgnoreCase(this));
}

所以必然会出现错误。你可以试试:

 public boolean equals(Name otherName){
    if(firstName.equalsIgnoreCase(otherName.firstName) && middleName.equalsIgnoreCase(otherName.middleName) && lastName.equalsIgnoreCase(otherName.lastName))
        return true;
    else 
        return false;
}
于 2012-12-11T03:56:15.717 回答
0
public boolean equals(Name otherName){
            return (otherName.getFirst().equalsIgnoreCase(firstName));
        }

您正在比较对象而不是字符串...

于 2012-12-11T04:47:00.550 回答