Java 如何在从控制台读取的通讯簿中检索信息

Java 如何在从控制台读取的通讯簿中检索信息,java,Java,我们班上有一本通讯录。我需要帮助的问题是,当用户选择按姓氏查找联系人信息时,输入姓氏后,所有联系人信息都会显示出来。如果给定的姓氏不在列表中,则会显示该消息。如果有超过1人,且姓氏已知,则显示所有姓名。我该怎么做呢 --------This is ContactList-------- /** * One object of this class is * responsible for everyone's contact

我们班上有一本通讯录。我需要帮助的问题是,当用户选择按姓氏查找联系人信息时,输入姓氏后,所有联系人信息都会显示出来。如果给定的姓氏不在列表中,则会显示该消息。如果有超过1人,且姓氏已知,则显示所有姓名。我该怎么做呢

          --------This is ContactList--------

       /**
      * One object of this class is
      * responsible for everyone's contact
      */
     import java.util.ArrayList;//A.M.
     import java.util.Collections;//A.M.

   public class ContactList  {//A.M.
public ArrayList<Contact> contacts;//A.M.
// creates arraylist
ContactList(){//A.M.
    contacts = new ArrayList<Contact>();//A.M.

}
// Add contact to array list
public void add(Contact contact){//A.M.
    contacts.add(contact);//A.M.

}

// calls and returns toString from class Contact
public String toString(){//A.M.
    String contactInfo = "";//A.M.
    for (Contact contact : contacts){//A.M.
        contactInfo += contact.toString() + "\n";//A.M.
    }
    return contactInfo;//A.M.
}
}


}

您的问题是什么?如何按姓氏检索联系人信息?
           ---------This is class Contact------------


/**
* One object of this class is 
 * responsible for one person's contact
 **/
import java.util.Scanner;//A.M.

 public class Contact   {//A.M.
private String firstName, lastName, address, city, state, zip, email,
        phoneNumber, notes;//A.M.
        Scanner scanner;//A.M.



// puts contact's information in the desired order
public String  toString() {//A.M.
    return firstName + " " + lastName + "\n" + address + "\n" + city + ", "
            + state + " " + zip + "\n" + email + "\n" + phoneNumber + "\n"
                + notes + "\n";//A.M.


}
/** Reads the contact information
 * takes user input and updates private members of class
 * @return boolean to read if method returns true if contact info was read successfully,
 * returns false otherwise
 */

public boolean read() {//A.M.A.J.
    scanner = new Scanner(System.in);//A.M.
    System.out.print("First Name : ");//A.M.
    firstName = scanner.nextLine();//A.M.
    System.out.print("Last Name : ");//A.M.
    lastName = scanner.nextLine();//A.M.

    if (lastName.equals("") ) {//A.J.
         return false;
     }

    System.out.print("Address : ");//A.M.
    address = scanner.nextLine();//A.M.
    System.out.print("City : ");//A.M.
    city = scanner.nextLine();//A.M.
    System.out.print("State : ");//A.M.
    state = scanner.nextLine();//A.M.
    System.out.print("Zip : ");//A.M.
    zip = scanner.nextLine();//A.M.
    System.out.print("Email Address : ");//A.M.
    email = scanner.nextLine();//A.M.
    System.out.print("Phone Number : ");//A.M.
    phoneNumber = scanner.nextLine();//A.M.
    System.out.print("Notes about the contact : ");//A.M.
    notes = scanner.nextLine();//A.M.
           return true;
}
                ---------This is Class TestContact----------------

/**
* One object of this class is 
* responsible for everyone's contact
 */
import java.util.Scanner;//A.M.

public class TestContact { // A.M.
public static void main(String args[]) {// A.M.
    Contact contact;// A.M.
    contact = new Contact();// A.M.
    ContactList contactList;// A.M.
    contactList = new ContactList();// A.M.
    Scanner scanner;// A.M.
    scanner = new Scanner(System.in);// A.M.
    int option = 1;// A.M.
    // calls scanner in class Contact
    boolean readSuccessful; // A.J.

    // While allows the switch statement to start over after an option is
    // selected
    while (option != 4) {// A.M.
        // Menu for the contact program
        System.out.println("Contact List");
        System.out.println("Add a new contact : 1");
        System.out
                .println("Search for an existing contact by last name : 2");
        System.out.println("Print your entire contact list : 3");
        System.out.println("Quit program : 4");

        // replaces
        option = scanner.nextInt();// A.M.
        switch (option) {// A.M.
        case 1:
            contact = new Contact();// A.M.
            readSuccessful = contact.read();// A.J.
            if (readSuccessful == false) {// A.J.
                System.out.println("Need to enter Last Name");// A.J.
            } else {
                contactList.add(contact);// A.M.
                System.out.println("Contact added to list \n");// A.M.
            }
            // Prints toString from array in class ContactList
            System.out.println(contactList.toString());// A.M.
            break;

        case 2:

            break;

        case 3:
            // Prints toString from array in class ContactList
            System.out.println(contactList.toString());// A.M.

        case 4:
            System.out.println("Program has been closed.");// A.M.
            break;

        default:
            // if wrong input is used it will give a warning
            System.out.println("Not a valid option.\n");// A.M.
            break;
        }

    }

}