Searching a vector within a collection class

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

c18drmAuthor Commented:
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
so, v is a Vector of Person objects?

what do you want to search by?  First name, Surname, etc?  Which one?
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

c18drmAuthor Commented:
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
c18drmAuthor Commented:
ok that makes sense but how do i get it to accept the text that i'm trying to search for?
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());
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...
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;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.