【问题标题】:Tweak output from an arraylist调整数组列表的输出
【发布时间】:2014-02-11 18:51:40
【问题描述】:

这是我需要满足的最后一个要求,我无法弄清楚我做错了什么。我试图仅显示输入到数组中的联系人的 ContactID、名字和姓氏。 我的父类中有一个函数,它设置为显示 ContactID、名字和姓氏。 我的问题是它确实输出了这些细节。但它会在打印联系人之前输出最后输入的联系人的完整详细信息。

这是我的主要课程:选项 3 是问题所在。

导入 java.util.ArrayList; 导入 java.util.Scanner;

公共类联系人列表 {

public static void main(String[] args) {

    ArrayList<Contact> contacts = new ArrayList<>();

    Scanner input1 = new Scanner(System.in);
    int type = 0;
    while(type != 5){
    System.out.println("Please select an option: ");
    System.out.println("Add a Personal Contact: Enter 1 ");
    System.out.println("Add a Business Contact: Enter 2 ");
    System.out.println("Display Contacts List: Enter 3 ");
    System.out.println("Display Contact Details: Enter 4 ");
    System.out.println("To Quit: Enter 5 ");

    type = input1.nextInt();

    if(type == 5){
        System.out.println("Goodbye ");
        break;
    }

如果(类型==1 || 类型==2){

     Contact contact = null;

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter ContactId : ");
    String contactId = input.nextLine();
    System.out.println("Please enter First Name : ");
    String firstName = input.nextLine();
    System.out.println("Please enter Last Name : ");
    String lastName = input.nextLine();
    System.out.println("Please enter Address : ");
    String address = input.nextLine();
    System.out.println("Please enter Phone Number : ");
    String phoneNumber = input.nextLine();
    System.out.println("Please enter Email Address : ");
    String emailAddress = input.nextLine();

    //Create a personal contact.
    if(type == 1){
       System.out.println("Please enter Birthday: ");
       String dateofBirth = input.nextLine();
       Contact pcontact = new PersonalContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, dateofBirth);
       contacts.add(pcontact);
    }
    //Create a business contact.
    else if(type == 2){
        System.out.println("Please enter Job Title: ");
        String jobTitle = input.nextLine();
        System.out.println("Please enter Organization: ");
        String organization = input.nextLine();
        Contact bcontact = new BusinessContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, jobTitle, organization);
        contacts.add(bcontact);
    }

}
    //Print full name of each Contact.
    if(type == 3){
        System.out.println(contacts);
        for (Contact namecontact: contacts)
        {
            System.out.println(namecontact.displayFullName());}
    } 
    //Print contact details for selected contact.
    else if(type == 4){
            System.out.println("Enter a Contact ID to display Contact Details: ");
            Scanner input2 = new Scanner(System.in);
            String soughtID;
            soughtID = input2.nextLine();
        for (Contact showcontact1: contacts)
            {
               if (showcontact1.displayId().equals(soughtID))
                System.out.println(showcontact1.displayContact());
            }
        }
     }
}

}

这是包含该方法的父类:displayFullName 是我正在调用的。

包ooo1;

公共抽象类联系人{

String contactId;
String firstName;
String lastName;
String address;
String phoneNumber;
String emailAddress;

public Contact(String contactId,String firstName,String lastName, String address, String phoneNumber, String emailAddress)
{
    this.contactId = contactId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.emailAddress = emailAddress;
}
public void setContactId(String input){
    this.contactId = input;
}
public String getContactId(){
    return contactId;
}

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

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

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

public void setPhoneNumber(String input){
    this.phoneNumber = input;
}
public String getPhoneNumber(){
    return phoneNumber;
}

public void setEmailAddress(String input){
    this.emailAddress = input;
}
public String getEmailAddress(){
    return emailAddress;
}

@Override
public String toString(){
   return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress: " + this.getAddress() + "\nPhone Number: " + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
}

public String displayFullName(){
    System.out.println("Contact: ");
    return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName());
}

public String displayContact(){
    return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress :" + this.getAddress() + "\nPhone Number :" + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
}
public String displayId(){
    return (this.getContactId());
}

}

这是输出的样子:

3 [联系人编号:3 名字:汤姆 姓名:琼斯 地址:西街 123 号 电话号码:345-235-2345 电子邮件地址:tjones@yahoo.com 出生日期:12-12-1978] 联系方式: 联络号码:3 名字:汤姆 姓氏:琼斯

当我打电话给这个时,我只想从联系人:(上面加粗)。

如何让它停止显示最后输入的联系人的完整详细信息。

【问题讨论】:

  • Dnt 在代码中发现任何问题以打印所有详细信息。您是否使用 toString 方法
  • 我可以打印所有详细信息,但问题是我无法仅打印我试图从 displayFullName() 调用的联系人 ID、名字、姓氏。它想打印出我在调用选项 3 时输入的最后一个联系人,然后它会在其后打印 displayFullName。就像在打印我的下一个电话之前需要离开以清除它所拥有的东西。
  • OP,你有想过这个吗?

标签: java oop printing arraylist


【解决方案1】:

删除下面的行,因为它正在打印联系人对象。

    if(type == 3){
        System.out.println(contacts);   // Remove this line
        for (Contact namecontact: contacts)
        {
            System.out.println(namecontact.displayFullName());
        } 

【讨论】:

  • @user3196742 请检查并告诉我
猜你喜欢
  • 2021-02-08
  • 1970-01-01
  • 2011-12-25
  • 2016-07-08
  • 1970-01-01
  • 2022-07-12
  • 1970-01-01
  • 2017-11-12
  • 2011-01-20
相关资源
最近更新 更多