Searching a vector within a collection class

Posted on 2004-11-12
Last Modified: 2010-03-31
Hi, writing a simple address book using a collection class.
All I want to do is read in info from a driver, then get it to Add a person, display all people, remove all people, search and display people.
Can get everything ok except the searching.
here's my code(i'll post the driver in a sec):

import java.util.*;
import java.text.*;

class Person{
    private String forename;
    private String surname;
    private String address;
    private String email;
    private String phoneNum;

    public Person (){}
    public Person (String forename, String surname, String address,
String email, String phoneNum){
      this.forename = forename;
      this.surname = surname;
  this.address = address; = email;
  this.phoneNum = phoneNum;

    public String getForename(){
      return forename;

    public String toString(){
      String displayAll;
      displayAll = forename + "\n" + surname + "\n" + address + "\n" + email
+ "\n" + phoneNum + "\n";
      return displayAll;

}   //end of class

class StorePeople{
    private Vector v;
    public StorePeople(){v = new Vector();}

    public void displayPeople(PrintWriter p){
      for( int n = 0; n < v.size(); n++)

    public void add(Person p){

    public void clear(){

    public void search(String searchText){

    if (v.contains(searchText))



Ignore the last method, search() as this is incorrect but this is the part im stuck on
Question by:c18drm

    Author Comment

    Now I'm reading in the inputs from this class:

    public class PersonWork{
    static BufferedReader keyboard = new
        BufferedReader ( new InputStreamReader (;
        static PrintWriter screen = new PrintWriter (System.out, true);

        public static void main (String[] args)
        throws IOException
          StorePeople p1 = new StorePeople ();
          runMenu (p1);
                StorePeople sp;
                int menuChoice;


        static void runMenu( StorePeople p1)
        throws IOException
          final int QUIT = 5;
          int choice;
              choice = getChoice();
              executeChoice(choice, p1);
          } while (choice != QUIT);

        static int  getInteger(){

          catch (Exception e){
              return 0;

        static void displayMenu(){
          screen.println ("1 = Add a person");
          screen.println ("2 = Display all people");
           screen.println ("3 = Search");
      screen.println ("4 = Clear book");
          screen.println ("5 = Quit");

        static int getChoice(){
          int choice;
          screen.println("Enter your selection:     ");
      choice = getInteger();
          return choice;

        static void executeChoice(int choice, StorePeople p1)
        throws IOException
      switch (choice){
          case 1: addPerson (p1);
          case 2: p1.displayPeople(screen);
      case 3: searchPeople (p1);
          case 4: p1.clear();

      static void addPerson( StorePeople p1)
        throws IOException
        Person person;
        String forename, surname, address, email, phoneNum;

        screen.println("Forename?   ");
        forename = keyboard.readLine(); screen.flush();
        screen.println("Surname?    ");
        surname = keyboard.readLine(); screen.flush();
        screen.println("Address?    ");
        address = keyboard.readLine(); screen.flush();
        screen.println("Email?    ");
        email = keyboard.readLine(); screen.flush();
        screen.println("Phone Number?    ");
        phoneNum = keyboard.readLine(); screen.flush();
        screen.println("Thanks for the entry");
        person = new Person(forename, surname, address, email, phoneNum);

        static void searchPeople (StorePeople p1)
          throws IOException
          Person person;
          String searchText;
          screen.println("Enter text to search for?   ");
          searchText = keyboard.readLine().trim();


    Again, ignore last method, I think its close to what I want to achieve but not perfect
    LVL 35

    Expert Comment

    so, v is a Vector of Person objects?

    what do you want to search by?  First name, Surname, etc?  Which one?
    LVL 35

    Accepted Solution

    You will basically have to iterate through the elements of the Vector, and check the Person object contained therin with your search criteria...

    for( int i = 0 ; i < v.size() ; i++ )
        Person p = (Person)v.elementAt( i ) ;
        //  check the required fields in p to see if they match the search criteria

    Author Comment

    yeah is vector of Person objects.
    say search by first name.
    ideally i'd like to be able to search, find the person and remove them

    Author Comment

    ok that makes sense but how do i get it to accept the text that i'm trying to search for?
    LVL 86

    Assisted Solution

    Ifyou use the following approach you will get a more efficient search [ O(log n) as opposed to O(n) ]

    String enteredText = "Jim";
    int indexOfJimEntry = Collections.binarySearch(v, new Person(enteredText, null, null, null, null), new Comparator() {
          public int compare(Object o1, Object o2) {
                Person p1 = (Person)o1;
                Person p2 = (Person)o2;
                return p1.getForename().compareTo(p2.getForename());

    Assisted Solution

    add Hashtable to your StorePeople class and hash Person by field that you'll serch by. In that case, you don't even need Vector, you can work with Hashtable all the time...
    LVL 92

    Assisted Solution

    In your store add a new member var for storing names:

    private Hashtable lookup = new Hashtable();

    then when you add a name, store it in the hastable using whatever you want to lookup by as the key, eg. to lookup by forename

    lookup.put(pgetForename(), p);

    Then in your search to lookup a person by forename use:

    public Person search(String forename)
      Person p = (Person) lookup.get(forename);
      return p;

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    nested class vs inner class 5 36
    stringclean challenge 26 43
    create a gui in perl 3 21
    @SBGen Method 3 16
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now