2017-03-31 30 views
0

私は学校授業の一環としてアドレス帳をやっていますが、私はそれを大部分は理解していますが、私は一部に固執しています。ここに私のコードがあります。私は変数を含むAddressBookクラスを持っていて、本の中の各人物の情報を変更/返すための適切なgetterメソッドとsetterメソッドを持っています。次に、アドレス帳の仕組みを示すメソッドmainを持つTestAddressBookクラスがあります。私が立ち往生している唯一のことは、最終的な要件です。ここでは、2つの名前を比較し、それらが等しいかどうかを確認して結果を表示するよう求められます。プログラムを私が望むように設計しましたが、何らかの理由で比較を行うと、比較結果が表示されません。ただ何も印刷しません。私は明らかな何かを見逃すはずです。ここにクラスのコードがあります。アドレス帳プログラム

アドレス帳...

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 AddressBook(String firstName) { 
    this.firstName = firstName; 
} 

public AddressBook(String firstName, String middleName) { 
    this.firstName = firstName; 
    this.middleName = middleName; 
} 

public AddressBook(String firstName, String middleName, String lastName) { 
    this.firstName = firstName; 
    this.middleName = middleName; 
    this.lastName = lastName; 
} 

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) { 
    String nameEquals; 
    if (name1.equals(name2)) { 
     nameEquals = (name1 + " and " + name2 + " are equal names."); 
     return nameEquals; 
    } 
    else { 
     nameEquals = (name1 + " and " + name2 + " are not equal names."); 
     return nameEquals; 
    } 
} 

}

テストクラス...

public class TestAddressBook { 

public static void main(String[] args) { 

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

    AddressBook entry1 = 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:  " + entry1.getFirstName()); 
    System.out.println("Middle Name:  " + entry1.getMiddleName()); 
    System.out.println("Last Name:  " + entry1.getLastName()); 
    System.out.println("Address:   " + entry1.getHomeAddress()); 
    System.out.println("Home Phone:  " + entry1.getHomePhone()); 
    System.out.println("Cell Phone:  " + entry1.getCellPhone()); 
    System.out.println("Business Phone: " + entry1.getBusinessPhone()); 
    System.out.println("Skype ID:   " + entry1.getSkypeId()); 
    System.out.println("Facebook ID:  " + entry1.getFacebookId()); 
    System.out.println("Personal Website: " + entry1.getPersonalWebSite()); 

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

    AddressBook entry2 = 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:  " + entry2.getFirstName()); 
    System.out.println("Middle Name:  " + entry2.getMiddleName()); 
    System.out.println("Last Name:  " + entry2.getLastName()); 
    System.out.println("Address:   " + entry2.getHomeAddress()); 
    System.out.println("Home Phone:  " + entry2.getHomePhone()); 
    System.out.println("Cell Phone:  " + entry2.getCellPhone()); 
    System.out.println("Business Phone: " + entry2.getBusinessPhone()); 
    System.out.println("Skype ID:   " + entry2.getSkypeId()); 
    System.out.println("Facebook ID:  " + entry2.getFacebookId()); 
    System.out.println("Personal Website: " + entry2.getPersonalWebSite()); 

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

    AddressBook entry3 = 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:  " + entry3.getFirstName()); 
    System.out.println("Middle Name:  " + entry3.getMiddleName()); 
    System.out.println("Last Name:  " + entry3.getLastName()); 
    System.out.println("Address:   " + entry3.getHomeAddress()); 
    System.out.println("Home Phone:  " + entry3.getHomePhone()); 
    System.out.println("Cell Phone:  " + entry3.getCellPhone()); 
    System.out.println("Business Phone: " + entry3.getBusinessPhone()); 
    System.out.println("Skype ID:   " + entry3.getSkypeId()); 
    System.out.println("Facebook ID:  " + entry3.getFacebookId()); 
    System.out.println("Personal Website: " + entry3.getPersonalWebSite()); 



    AddressBook.compareNames(entry1.getFirstName() + " " + 
          entry1.getMiddleName().charAt(0) + " " + 
          entry1.getLastName(), 
          entry2.getFirstName() + " " + 
          entry2.getMiddleName().charAt(0) + " " + 
          entry2.getLastName()); 







} 

}

+0

また、私のAddressBookクラスのcompareNamesメソッドは何も印刷しないと明示的に指示しているだけで、比較の値を返します。だから私は私のテストクラスのメソッド呼び出しからそれを印刷する必要があります。つまり、「名前1と名前1が等しい名前ではない」のようなものです – ripp2k

+0

あなたはあなたのコードに 'compareNames'の結果を出力しようとしていません。 – UnholySheep

+0

テストクラスで名前比較の結果を印刷するにはどうすればいいですか? – ripp2k

答えて

0

だけでなく、あなたがこの

package teteo; 

public class TestAddressBook { 

    public static void main(String[] args) { 

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

     AddressBook entry1 = 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:  " + entry1.getFirstName()); 
     System.out.println("Middle Name:  " + entry1.getMiddleName()); 
     System.out.println("Last Name:  " + entry1.getLastName()); 
     System.out.println("Address:   " + entry1.getHomeAddress()); 
     System.out.println("Home Phone:  " + entry1.getHomePhone()); 
     System.out.println("Cell Phone:  " + entry1.getCellPhone()); 
     System.out.println("Business Phone: " + entry1.getBusinessPhone()); 
     System.out.println("Skype ID:   " + entry1.getSkypeId()); 
     System.out.println("Facebook ID:  " + entry1.getFacebookId()); 
     System.out.println("Personal Website: " + entry1.getPersonalWebSite()); 

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

     AddressBook entry2 = new AddressBook("Robert", "James", "Smith", "56 Outtamy Way", 
              "564-342-8372", "564-283-9832", 
              "564-293-3489", "brucie392", "brucieface28", 
              "http://www.freewebhost.com/carteristheman"); 

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

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

     AddressBook entry3 = 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:  " + entry3.getFirstName()); 
     System.out.println("Middle Name:  " + entry3.getMiddleName()); 
     System.out.println("Last Name:  " + entry3.getLastName()); 
     System.out.println("Address:   " + entry3.getHomeAddress()); 
     System.out.println("Home Phone:  " + entry3.getHomePhone()); 
     System.out.println("Cell Phone:  " + entry3.getCellPhone()); 
     System.out.println("Business Phone: " + entry3.getBusinessPhone()); 
     System.out.println("Skype ID:   " + entry3.getSkypeId()); 
     System.out.println("Facebook ID:  " + entry3.getFacebookId()); 
     System.out.println("Personal Website: " + entry3.getPersonalWebSite()); 



     String equal = AddressBook.compareNames(entry1.getFirstName() + " " + 
           entry1.getMiddleName().charAt(0) + " " + 
           entry1.getLastName(), 
           entry2.getFirstName() + " " + 
           entry2.getMiddleName().charAt(0) + " " + 
           entry2.getLastName()); 

System.out.println(equal); 


} 
} 
で、それは良い顔をしています

今、それは等しいですが、これを実行するために、あなたはこの

マップを使ったらどうなるか

https://www.javacodegeeks.com/2013/03/difference-between-comparator-and-comparable-in-java.html

のcompareTo()または同等の外観を使用する必要があることを知っている必要があります?それはキーに依存しますが、重複する値が得られます。コードを使用することはできませんが、使用したい場合は、そこを目で確認してください:)

+0

これはまさに私が必要としていたものです!私は何かが明らかでないことを知っていた。今はすべて意味があります。ありがとうございました! – ripp2k

+0

完了........... – ripp2k

関連する問題