Java - 从多个对象读取



我正在尝试创建一个基本的地址簿,我对如何让地址簿类从 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
public static void compareNames(String name1, String name2) {

你可能的意思是

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

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

看起来您想有一种方法来比较两个地址簿实例。在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
}

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

您正在尝试调用getFirstName()getMiddleName()getLastName() Strings这没有意义,因为String没有这些方法。只有您的AddressBook类才能这样做。相反,您应该将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方法说它返回String,但它没有返回任何内容。它要么需要返回String,要么将返回类型更改为 void

相关内容

  • 没有找到相关文章

最新更新