【问题标题】:Not getting expected output没有得到预期的输出
【发布时间】:2015-02-21 04:38:48
【问题描述】:

当我运行我的程序并选择选项 2 来获取联系方式时,我希望它显示如下(名字、姓氏):

Contacts how have been entered: 
0) John Doe 
1) George Smith 
2) Nancy Davis 
Please enter the number corresponding to the contact you would like to view: 

对于个人联系人,它显示如下:

Contacts who have been entered: 
0) Doe 1 F St. (last name, address) 
Please enter the number corresponding to the contact you would like to view: 

对于业务联系人,它显示如下:

Contacts who have been entered: 
0) 1 F St. jd@gmail.com (address, email) 
Please enter the number corresponding to the contact you would like to view: 

然后,当我输入数字以显示个人联系人时,它只返回我的名字,而企业只返回我的名字和姓氏。它应该返回在添加联系人步骤中输入的完整联系信息。我以为我对所有内容都进行了正确编程,但它没有显示我想看到的内容。任何帮助将不胜感激。我的代码在下面列出。

主要:

package contactlist;

import java.util.ArrayList;
import java.util.Scanner;


public class ContactList {

    public static void main(String[] args) {

    int swValue;
    Scanner keyIn = new Scanner(System.in);

    ArrayList<Contact> ContactRecords = new ArrayList<Contact>();
    while (true) {
        // Display menu graphics
        System.out.println("========================================");
        System.out.println("|            Address List              |");
        System.out.println("========================================");
        System.out.println("| Options:                             |");
        System.out.println("|   1. Add Contact                     |");
        System.out.println("|   2. Get Contact Details             |");
        System.out.println("|   3. Exit                            |");
        System.out.println("========================================");
        System.out.println(" Select option: ");
        swValue = keyIn.nextInt();


        switch (swValue) {
            case 1:
                addContact(ContactRecords);
                break;
            case 2:
                getRecords(ContactRecords);
                break;
            case 3:
                System.exit(0);
                break;
            default:
                System.out.println("Invalid selection");
                break;
        }
    }
}

public static void addContact(ArrayList<Contact> ContactRecords) {
    Scanner textIn = new Scanner(System.in);
    Scanner keyIn = new Scanner(System.in);
    System.out.println("First Name: ");
    String firstName = textIn.nextLine();
    System.out.println("Last Name: ");
    String lastName = textIn.nextLine();
    System.out.println("Address:  ");
    String address = textIn.nextLine();
    System.out.println("Email Address: ");
    String email = textIn.nextLine();
    System.out.println("Phone: ");
    String phone = textIn.nextLine();
    System.out.println("Is this a 1) Personal or 2) Business?");
    int choice = keyIn.nextInt();
    if (choice == 1) {
        System.out.println("Date of Birth:  ");
        String dateOfBirth = textIn.nextLine();
        Personal aPersonal = new Personal(firstName, lastName, address,
        email, phone, dateOfBirth);
        ContactRecords.add(aPersonal);
    }
    if (choice == 2) {
        System.out.println("Job Title:  ");
        String jobTitle = textIn.nextLine();
        System.out.println("Organization: ");
        String organization = textIn.nextLine();
        Business aBusiness = new Business(firstName, lastName, address,
        email, phone, jobTitle, organization);
        ContactRecords.add(aBusiness);
    }

}

public static void getRecords(ArrayList<Contact> ContactRecords)
{
    Scanner keyIn = new Scanner(System.in);
    System.out.println("Contacts who have been entered:");
    for (int i = 0; i < ContactRecords.size(); i++) {
        System.out.println(i + ") "+ ContactRecords.get(i).getFirstName() +
        " " + ContactRecords.get(i).getLastName();
    }
    System.out.println("Please enter the number corresponding to the contact 
    you would like to view: ");
    int choice = keyIn.nextInt();

    System.out.println(ContactRecords.get(choice).toString());
}

}

联系方式

package contactlist;

public abstract class Contact {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;

public Contact(String firstName, String lastName, String address, String 
email, String phone) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.email = email;
    this.phone = phone;
}

public Contact() {
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

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

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

@Override
public String toString() {
    return toString() ;
}
}

个人

package contactlist;

public class Personal extends Contact {
private String dateOfBirth;

public Personal(String dateOfBirth, String firstName, String lastName, 
String address, String email, String phone) {
    super(firstName, lastName, address, email, phone);
    this.dateOfBirth = dateOfBirth;
}

public Personal() {
    super();
}

public String getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

@Override
public String toString() {
    return dateOfBirth;
}

}

商业

package contactlist;

public class Business extends Contact {
private String jobTitle;
private String organization;

public Business(String jobTitle, String organization, String firstName, 
String lastName, String address, String email, String phone) {
    super(firstName, lastName, address, email, phone);
    this.jobTitle = jobTitle;
    this.organization = organization;
}

public Business() {
    super();
}

public String getJobTitle() {
    return jobTitle;
}

public void setJobTitle(String jobTitle) {
    this.jobTitle = jobTitle;
}

public String getOrganization() {
    return organization;
}

public void setOrganization(String organization) {
    this.organization = organization;
}

@Override
public String toString() {
    return jobTitle + " " + organization;
}

}

【问题讨论】:

  • 如果你给我们一个更小的例子来重现你的问题,这将更方便每个人阅读。
  • 谢谢大家,我稍后会查看并相应更新。感谢您的意见。

标签: java


【解决方案1】:

这是您用来打印联系人的行:

System.out.println(ContactRecords.get(choice).toString());

这意味着您正在使用toString() 方法打印联系人。现在,您的个人联系人中有哪些内容?

@Override
public String toString() {
    return dateOfBirth;
}

这意味着它只返回出生日期,而不是联系人中的任何其他字段。

对于您的业务联系人:

@Override
public String toString() {
    return jobTitle + " " + organization;
}

这意味着它将仅显示字段 jobTitleorganization 的内容,而不显示任何其他字段。

这与@femtoRgon 给您的答案相结合,这意味着您也没有正确分配字段,从而为您提供了您所拥有的结果。

你必须:

  1. 更改联系人中的toString() 以返回常用字段。现在它是一种危险的、无限递归的方法:

    @Override
    public String toString() {
         return toString() ;
    }
    
  2. 重写个人和名片中的 toString 方法,以便它们返回 super.toString() 的组合 - 您在步骤 1 中更改的方法 - 以及特定于 Personal 的其他字段或Business.

  3. 更改您进行的new 调用,以便将参数正确传递给构造函数。

【讨论】:

  • RealSkeptic 非常感谢,现在一切正常。
【解决方案2】:

这里是 Personal 构造函数调用和 Personal 构造函数定义。

Personal aPersonal = new Personal(firstName, lastName, address,
    email, phone, dateOfBirth);

public Personal(String dateOfBirth, String firstName, String lastName, 
String address, String email, String phone) {

他们的参数顺序不匹配。所以你传入一个名为“firstName”的变量作为指定为“dateOfBirth”的参数,“lastName”作为“firstName”,“address”作为“lastName”等。所以你对ctor的调用应该是:

Personal aPersonal = new Personal(dateOfBirth, firstName, lastName, address,
    email, phone);

问题与您调用Business 构造函数非常相似,您将“jobTitle”和“organization”作为最后一个参数传入,而该类希望它们是前两个参数。

【讨论】:

  • 此外,如果这是 OP 的常见问题(使用长构造函数),创建空对象并仅使用 set 方法是有效的。
  • femtoRgon 感谢您指出我的粗心。再加上 RealSkeptic 的帮助,我现在正在工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
相关资源
最近更新 更多