Java - 从多个对象读取

Java - Reading from several objects

我正在尝试创建一个基本的 AddressBook,但我对如何让 AddressBook class 从 Main() 中创建的两个对象进行读取感到有点困惑。

我创建了一个 compareNames() 方法,但是如您所见,它不起作用。读取两个条目名称的方法是什么?我是否需要创建一个新的临时变量来存储名称?

这是我目前的情况:

import java.util.Scanner;
class AddressBookDemo {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    AddressBook name1 = new AddressBook("firstName", "middleName", "lastName", "homeAddress", "businessPhone",
            "homePhone", "cellPhone", "skypeId", "facebookId", "personalWebSite");
    AddressBook name2 = new AddressBook("firstName", "middleName", "lastName", "homeAddress", "businessPhone",
            "homePhone", "cellPhone", "skypeId", "facebookId", "personalWebSite");

    System.out.print("First name: ");
    name1.setFirstName(sc.nextLine());
    System.out.print("Middle name: ");
    name1.setMiddleName(sc.nextLine());
    System.out.print("Last name: ");
    name1.setLastName(sc.nextLine());
    System.out.print("Home address: ");
    name1.setHomeAddress(sc.nextLine());
    System.out.print("Business phone number: ");
    name1.setBusinessPhone(sc.nextLine());
    System.out.print("Home phone number: ");
    name1.setHomePhone(sc.nextLine());
    System.out.print("Cell phone number: ");
    name1.setCellPhone(sc.nextLine());
    System.out.print("Skype ID: ");
    name1.setSkypeId(sc.nextLine());
    System.out.print("Facebook ID: ");
    name1.setFacebookId(sc.nextLine());
    System.out.print("Personal Website: ");
    name1.setPersonalWebSite(sc.nextLine());

    System.out.println("==============================");

    System.out.print("First name: ");
    name2.setFirstName(sc.nextLine());
    System.out.print("Middle name: ");
    name2.setMiddleName(sc.nextLine());
    System.out.print("Last name: ");
    name2.setLastName(sc.nextLine());
    System.out.print("Home address: ");
    name2.setHomeAddress(sc.nextLine());
    System.out.print("Business phone number: ");
    name2.setBusinessPhone(sc.nextLine());
    System.out.print("Home phone number: ");
    name2.setHomePhone(sc.nextLine());
    System.out.print("Cell phone number: ");
    name2.setCellPhone(sc.nextLine());
    System.out.print("Skype ID: ");
    name2.setSkypeId(sc.nextLine());
    System.out.print("Facebook ID: ");
    name2.setFacebookId(sc.nextLine());
    System.out.print("Personal Website: ");
    name2.setPersonalWebSite(sc.nextLine());
    } // end of Main
} // End of class AddressBookDemo

import java.util.Scanner;
public class AddressBook {

Scanner sc = new Scanner(System.in);

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

// Declare public variables
public AddressBook(String firstName, String middleName, String lastName, String homeAddress, String businessPhone,
        String homePhone, String cellPhone, String skypeId, String facebookId, String personalWebSite) {
    this.firstName = firstName;
    this.middleName = middleName;
    this.lastName = lastName;
    this.homeAddress = homeAddress;
    this.businessPhone = businessPhone;
    this.homePhone = homePhone;
    this.cellPhone = cellPhone;
    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;
// }

// Setters & Getters
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getFirstName() {
    return firstName;
}

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

public String getMiddleName() {
    return middleName;
}

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

public String getLastName() {
    return lastName;
}

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

public String getHomeAddress() {
    return homeAddress;
}

public void setBusinessPhone(String businessPhone) {
    boolean invalidInput = false;
    do {
        this.businessPhone = sc.nextLine();
        invalidInput = false;
        try {
            long l = Long.parseLong(businessPhone.trim());
        } catch (NumberFormatException nfe) {
            invalidInput = true; // Test for invalid input
            System.out.println("NumberFormatException: Enter only numbers.");
        }
    } while (invalidInput == true);
}

public String getBusinessPhone() {
    return businessPhone;
}

public void setHomePhone(String homePhone) {
    boolean invalidInput = false;
    do {
        this.homePhone = sc.nextLine();
        invalidInput = false;
        try {
            long l = Long.parseLong(homePhone.trim());
        } catch (NumberFormatException nfe) {
            invalidInput = true; // Test for invalid input
            System.out.println("NumberFormatException: Enter only numbers.");
        }
    } while (invalidInput == true);
}

public String getHomePhone() {
    return homePhone;
}

public void setCellPhone(String cellPhone) {
    boolean invalidInput = false; // Test for invalid input
    do {
        this.cellPhone = sc.nextLine();
        invalidInput = false;
        try {
            long l = Long.parseLong(cellPhone.trim());
        } catch (NumberFormatException nfe) {
            invalidInput = true; // Test for invalid input
            System.out.println("NumberFormatException: Enter only numbers.");
        }
    } while (invalidInput == true);
}

public String getCellPhone() {
    return cellPhone;
}

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

public String getSkypeId() {
    return skypeId;
}

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

public String getFacebookId() {
    return facebookId;
}

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

public String getPersonalWebSite() {
    return personalWebSite;
}

// Compare names
public static String compareNames(String name1, String name2) {

    String comp1 = name1.getfirstName() + " " + name1.getMiddleName().toUpperCase().charAt(0) + ". "
            + name1.getLastName();
    String comp2 = name2.getFirstName() + " " + name2.getMiddleName().toUpperCase().charAt(0) + ". "
            + name2.getLastName();
    System.out.println("Comparing: " + comp1 + " and " + comp2);
    if (comp1.equalsIgnoreCase(comp2)) {
        System.out.println("The names match!");
    } else {
        System.out.println("The names don't match!");
    } // end if/else statement
}

} // end of class AddressBook

您似乎想要一种方法来比较两个 AddressBook 实例。在 Java 中执行此操作的正确方法是让 AddressBook 实现 Comparable<AddressBook>,然后实现 compareTo(),如下所示:

public int compareTo(AddressBook other)
{
    //return -1 if this is smaller than other
    //return 1 if this is larger than other
    //return 0 if this is equal to other
}

您现在可以使用此方法将 AddressBook 的一个实例与另一个实例进行比较。另一个好处是您现在还可以使用 Collections.sort() 对地址簿列表进行排序。

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

你的意思可能是

public static void compareNames(AddressBook name1, AddressBook name2) {

注意 return 类型已更改为 void。要恢复到 String,请考虑将 System.out.println 语句更改为 return 语句。

您正在尝试在 Strings 上调用 getFirstName()getMiddleName()getLastName(),这没有意义,因为 String 没有那些方法。只有您的 AddressBook class 可以。您应该将 AddressBook 的实例传递给 compareNames() 方法:

// Compare names
public static String compareNames(AddressBook name1, AddressBook name2) {
    ...
}

然后您可以从 main 调用它,例如:

AddressBook.compareNames(name1, name2);

更好的是,您可以使 compareNames 成为非静态的,然后只传递一个 AddressBook 来与 this:

进行比较
// Compare names
public String compareNames(AddressBook other) {

    String comp1 = this.getfirstName() + " " + this.getMiddleName().toUpperCase().charAt(0) + ". "
            + this.getLastName();
    String comp2 = other.getFirstName() + " " + other.getMiddleName().toUpperCase().charAt(0) + ". "
            + other.getLastName();
    System.out.println("Comparing: " + comp1 + " and " + comp2);
    if (comp1.equalsIgnoreCase(comp2)) {
        System.out.println("The names match!");
    } else {
        System.out.println("The names don't match!");
    } // end if/else statement
}

然后您可以从 main 调用它,例如:

name1.compareNames(name2);

此外,您的 compareNames 方法说它 returns String,但它没有 returning 任何东西。它需要 return 一个 String 或将 return 类型更改为 void.