比较地址簿程序中的名称

我正在做一本地址簿作为学校作业的一部分而且我已经把它弄清楚了,但我只是坚持了一部分 . 这是我的代码 . 我有一个AddressBook类,它包含变量和适当的getter和setter方法,用于更改/返回书中每个人的信息 . 然后我有一个带有方法main的TestAddressBook类,它演示了如何处理书籍的工作 . 我唯一坚持的是最终要求,我被要求比较两个名字,看看它们是否相等 . 这是类的代码 .

public class AddressBook {

private String firstName;
private String middleName;
private String lastName;
private String homeAddress;
private String homePhone;
private String cellPhone;
private String businessPhone;
private String skypeId;
private String facebookId;
private String personalWebSite;

public AddressBook(String firstName, String middleName, String lastName,
                   String homeAddress, String homePhone, String cellPhone,
                   String businessPhone, String skypeID, String facebookID,
                   String personalWebSite) {

                        this.firstName = firstName;
                        this.middleName = middleName;
                        this.lastName  = lastName;
                        this.homeAddress = homeAddress;
                        this.homePhone = homePhone;
                        this.cellPhone = cellPhone;
                        this.businessPhone = businessPhone;
                        this.skypeId = skypeID;
                        this.facebookId = facebookID;
                        this.personalWebSite = personalWebSite;
                        }

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getMiddleName() {
    return middleName;
}

public void setMiddleName(String middleName) {
    this.middleName = middleName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getHomeAddress() {
    return homeAddress;
}

public void setHomeAddress(String homeAddress) {
    this.homeAddress = homeAddress;
}

public String getHomePhone() {
    return homePhone;
}

public void setHomePhone(String homePhone) {
    this.homePhone = homePhone;
}

public String getCellPhone() {
    return cellPhone;
}

public void setCellPhone(String cellPhone) {
    this.cellPhone = cellPhone;
}

public String getBusinessPhone() {
    return businessPhone;
}

public void setBusinessPhone(String businessPhone) {
    this.businessPhone = businessPhone;
}

public String getSkypeId() {
    return skypeId;
}

public void setSkypeId(String skypeId) {
    this.skypeId = skypeId;
}

public String getFacebookId() {
    return facebookId;
}

public void setFacebookId(String facebookId) {
    this.facebookId = facebookId;
}

public String getPersonalWebSite() {
    return personalWebSite;
}

public void setPersonalWebSite(String personalWebSite) {
    this.personalWebSite = personalWebSite;
}

public static String compareNames(String name1, String name2) {

}

}

那我的考试班......

public class TestAddressBook {

public static void main(String[] args) {

    System.out.println("ENTRY 1");

    AddressBook name1 = new AddressBook("Robert", "James", "Smith", "3 Fake St", 
                                        "222-321-8371", "222-423-2382", 
                                        "222-438-2918", "bob483", "bobfb493",
                                        "http://www.freewebhost.com/bob848");

    System.out.println( "First Name:        " + name1.getFirstName() );
    System.out.println( "Middle Name:       " + name1.getMiddleName() );
    System.out.println( "Last  Name:        " + name1.getLastName() );
    System.out.println( "Address:           " + name1.getHomeAddress() );
    System.out.println( "Home Phone:        " + name1.getHomePhone() );
    System.out.println( "Cell Phone:        " + name1.getCellPhone() );
    System.out.println( "Business Phone:    " + name1.getBusinessPhone() );
    System.out.println( "Skype ID:          " + name1.getSkypeId() );
    System.out.println( "Facebook ID:       " + name1.getFacebookId() );
    System.out.println( "Personal Website:  " + name1.getPersonalWebSite() );

    System.out.println("\nENTRY 2");

    AddressBook name2 = new AddressBook("Bruce", "Allan", "Carter", "56 Outtamy Way", 
                                        "564-342-8372", "564-283-9832", 
                                        "564-293-3489", "brucie392", "brucieface28",
                                        "http://www.freewebhost.com/carteristheman");

    System.out.println( "First Name:        " + name2.getFirstName() );
    System.out.println( "Middle Name:       " + name2.getMiddleName() );
    System.out.println( "Last  Name:        " + name2.getLastName() );
    System.out.println( "Address:           " + name2.getHomeAddress() );
    System.out.println( "Home Phone:        " + name2.getHomePhone() );
    System.out.println( "Cell Phone:        " + name2.getCellPhone() );
    System.out.println( "Business Phone:    " + name2.getBusinessPhone() );
    System.out.println( "Skype ID:          " + name2.getSkypeId() );
    System.out.println( "Facebook ID:       " + name2.getFacebookId() );
    System.out.println( "Personal Website:  " + name2.getPersonalWebSite() );

    System.out.println("\nENTRY 3");

    AddressBook name3 = new AddressBook("Susan", "Anne", "Peters", "6 Madeup Blvd", 
                                        "736-453-1238", "736-392-2385", 
                                        "736-926-2439", "anniep", "susananne",
                                        "http://www.freewebhost.com/Susanspage");

    System.out.println( "First Name:        " + name3.getFirstName() );
    System.out.println( "Middle Name:       " + name3.getMiddleName() );
    System.out.println( "Last  Name:        " + name3.getLastName() );
    System.out.println( "Address:           " + name3.getHomeAddress() );
    System.out.println( "Home Phone:        " + name3.getHomePhone() );
    System.out.println( "Cell Phone:        " + name3.getCellPhone() );
    System.out.println( "Business Phone:    " + name3.getBusinessPhone() );
    System.out.println( "Skype ID:          " + name3.getSkypeId() );
    System.out.println( "Facebook ID:       " + name3.getFacebookId() );
    System.out.println( "Personal Website:  " + name3.getPersonalWebSite() );






}

}

作业要求这个......

“使用get和set方法,创建比较方法compareNames(name1,name2),比较字符串name1和name2的第一个,中间名和最后一个名 . 假设name1和name2遵循以下格式:”FirstName M. LastName“ “ .

这项任务要求对这两个字符串进行比较,然后返回“罗伯特·J·史密斯和布鲁斯·卡特不同名”的字样 . 有人能帮忙吗?

回答(3)

2 years ago

我刚刚意识到这里没有排序请求......所以最简单的答案是,考虑空值

public  boolean compareNames( AddressBook a, AddressBook b) {
    if ( a == null || b == null ) {
       return false;
    }

    return Objects.equals( a.getFirstname(), b.getFirstname() )
           && Objects.equals( ... ); // fill in the rest
}

你的规范没有说明 Susan Anne PetersonSusan Alice Peterson 应该是同一个人(如果他们应该对中间名称进行子串,它通过缩短它来暗示这一点) . 使用 Objects.equals 因为它是空的安全 .

可以说,如果这真的是你想要的域等价,你应该在 AddressBook 上实现 equalshashCode ,正确的方法是看this article on equality . 如果您决定实现equals,那么您可以使用EqualsVerifier验证其正确性

如果由于某种原因你想要比较/排序,以不区分大小写的null安全方式

Comparator<String> nsci =Comparator.nullsFirst(String.CASE_INSENSTIVE_ORDER)
return Comparator.comparing(AddressBook::getFirstName, nsci )
   .thenComparing(AddressBook::getMiddleName, nsci )
   .thenComparing(AddressBook::getLastName, nsci ).compareTo( address1, address2 ); // if 0 they are equal

2 years ago

首先,让我们将名称格式化为“Firstname MiddleInitial.Lastname”,以便轻松比较它们 . 这可以使用以下代码完成:

//Get each part of the name:
String firstname1 = entry1.getFirstName();
String middleInitial1 = entry1.getMiddleName().charAt(0);
String lastname1 = entry1.getLastName();
//Combine them
String fullname1 = firstname + " " + middleInitial + ". " + lastname;

如果你想让它更紧凑,你可以组合线条 .

String fullname1 = entry1.getFirstName() + " " + entry1.getMiddleName().chatAt(0) + ". " + entry1.getLastName();

对第二个条目重复此步骤,只需更改变量名称 .

现在,让我们比较两个字符串 . 这可以通过简单的 if 语句完成,例如:

if (fullname1.equalsIgnoreCase(fullname2) {
    System.out.println(fullname1 + " is equal to " + fullname2);
} else {
    System.out.println(fullname1 + " is not equal to " + fullname2);
}

现在你已经拥有了代码,让我们把它变成一个方法 . 这可以通过以下方式完成:

public static String (AddressBook entry1 AddressBook entry2) {
  String fullname1 = entry1.getFirstName() + " " + entry1.getMiddleName().charAt(0) + ". " + entry1.getLastName();
  String fullname2 = entry1.getFirstName() + " " + entry1.getMiddleName().charAt(0) + ". " + entry1.getLastName();
  if (fullname1.equalsIgnoreCase(fullname2) {
        return fullname1 + " is equal to " + fullname2;
    } else {
        return fullname1 + " is not equal to " + fullname2;
    }
}

2 years ago

要比较两个字符串的内容,只需使用 string1.equals(string2); 将返回一个布尔值 . 除此之外,您可能希望先清理字符串 . 您可以通过调用字符串上的 .CASE_INSENSITIVE_ORDER 常量来获取这两个名称并比较它们的小写形式 . 希望这有帮助,我没有给予太多