Search code examples
javaobjectprogram-entry-point

Java - Reading from several objects


I'm trying to create a basic AddressBook and I'm a bit stumped on how to get the AddressBook class to read from two objects created in the Main().

I created a compareNames() method however as you can see it doesn't work. What's the way to read the names of both entries? Do I need to create a new temporary variable to store the names into?

Here's what I have thus far:

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

And

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

Solution

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

    you probably meant

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

    NOTE that the return type was changed to void. To get it back to String, consider changing System.out.println statements into return statements.