[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Searching a vector within a collection class

Posted on 2004-11-12
11
Medium Priority
?
191 Views
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.io.*;
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;
  this.email = 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++)
          p.println(v.elementAt(n));
    }

    public void add(Person p){
      v.addElement(p);
    }

    public void clear(){
      v.removeAllElements();
    }

    public void search(String searchText){

    if (v.contains(searchText))
    System.out.println("yes");
    else
    System.out.println("no");

    }


}



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

Author Comment

by:c18drm
ID: 12566687
Now I'm reading in the inputs from this class:

import java.io.*;
public class PersonWork{
static BufferedReader keyboard = new
    BufferedReader ( new InputStreamReader (System.in));
    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;
      do{
          displayMenu();
          choice = getChoice();
          executeChoice(choice, p1);
      } while (choice != QUIT);
    }


    static int  getInteger(){
      try{
          return(Integer.parseInt(keyboard.readLine().trim()));

      }
      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);
          break;
      case 2: p1.displayPeople(screen);
          break;
  case 3: searchPeople (p1);
      break;
      case 4: p1.clear();
          break;
      }
    }

  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();
    screen.println("Thanks for the entry");
    screen.println();
    person = new Person(forename, surname, address, email, phoneNum);
    p1.add(person);
    }





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

}





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

Expert Comment

by:TimYates
ID: 12566700
so, v is a Vector of Person objects?

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

Accepted Solution

by:
TimYates earned 500 total points
ID: 12566728
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
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:c18drm
ID: 12566747
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
0
 

Author Comment

by:c18drm
ID: 12566788
ok that makes sense but how do i get it to accept the text that i'm trying to search for?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 12567361
Ifyou use the following approach you will get a more efficient search [ O(log n) as opposed to O(n) ]

String enteredText = "Jim";
Collections.sort(v);
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());
      }
});
0
 

Assisted Solution

by:mudri
mudri earned 500 total points
ID: 12568506
Hint:
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...
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 12569630
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;
}
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

873 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