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

    Default Search for object by keyword

    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;
    Contact otherContact = (Contact)otherObject;
    return (first.equals(otherContact.first) ||

    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
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Search for object by keyword

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

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 4
    Last Post: 03-25-2011, 12:50 AM
  2. Replies: 0
    Last Post: 10-29-2009, 10:28 AM
  3. Replies: 5
    Last Post: 08-26-2008, 04:43 PM
  4. Search a object in a vector
    By TalhaS in forum New To Java
    Replies: 2
    Last Post: 04-30-2008, 03:05 PM
  5. Replies: 0
    Last Post: 04-04-2008, 10: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