我试图创建一个基本的AddressBook,我对如何让AddressBook类从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
答案 0 :(得分:1)
看起来您想要一种比较两个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()
对AddressBooks列表进行排序。
答案 1 :(得分:1)
public static void compareNames(String name1, String name2) {
你可能意味着
public static void compareNames(AddressBook name1, AddressBook name2) {
注意,返回类型已更改为void
。要将其恢复为String
,请考虑将System.out.println
语句更改为return
语句。
答案 2 :(得分:1)
您正试图在getFirstName()
上致电getMiddleName()
,getLastName()
和Strings
这是没有意义的,因为String
没有&#39;有那些方法。只有你的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
。