Results 1 to 2 of 2
  1. #1
    zorrinna is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Search for object by keyword

    Hi,
    I have an ArrayList of objects type Contact. Every Contact contains first name, last name, phone number and e-mail (all of them String).
    I have to write a method which search for a keyword in the records and return true if this keyword has been found anywhere in it. The method I came up with is:
    public boolean match(Object otherObject)
    {
    if (otherObject ==null)
    {
    return false;
    }
    else if (getClass() != otherObject.getClass())
    {
    return false;
    }
    else
    {
    Contact otherContact = (Contact)otherObject;
    return (first.equals(otherContact.first) ||
    last.equals(otherContact.last)||
    phone.equals(otherContact.phone)||
    email.equals(otherContact.email));
    }
    }

    and it works fine. The problem is that in this case it returns true only if the two values are exactly the same. I need to modify it in a way that even if the keyword is "ohn" records with name "John" to return true. It has to work with any keyword and to search for it in all object variables. Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Search for object by keyword

    Use String.contains( ... ) instead of String.equals( ... ).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  2. Replies: 0
    Last Post: 10-29-2009, 09:28 AM
  3. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  4. Search a object in a vector
    By TalhaS in forum New To Java
    Replies: 2
    Last Post: 04-30-2008, 02:05 PM
  5. Replies: 0
    Last Post: 04-04-2008, 09:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •