Results 1 to 8 of 8
  1. #1
    scurvyjones is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Create a Search Button - Java

    I need to create a search button for a Mock Library Database.
    The Items(books, cds, dvds, and magazines) are in an array list.
    If a user inputs "Tom" the search button should return any Items that have the String "Tom" in them.

    Any helps would be greatly appreciated.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    We will gladly help you. Do you have a specific question that we can answer for you? To get better help, though you may wish to read the second link in my signature links below on asking smart questions as it will have lots of tips to help you ask questions that we can actually answer and give helpful responses.

    Much luck and welcome to our forum!

  3. #3
    scurvyjones is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the links!
    As I was writing the question I was having a hard time trying to figure out what to write.
    I hope the links will help me write a more accurate questions as to what I'm looking for.

    Thanks again

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    As I understand, you need to declare another ArrayList, then loop trough the ArrayList, which contains the items, and on each item call the '.contains(String userInput)', if it returns true, add the item to your new ArrayList.

  6. #6
    gayankan is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    search the new input in the array list ,

    ArrayList ar = new ArrayList();
    ar.add( "X" );
    ar.add( "Y" );

    for( int i = 0; i < ar.size(); i++ )
    {
    if( ar.get(i) == "X" )
    {
    System.out.println( "OK" );
    }
    }
    or intead of == you can use if( ar.get(i).equals( "X")) as well

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    @gayankan, learn to use the code tags for posting code.
    or intead of == you can use if( ar.get(i).equals( "X")) as well
    No. Unless the objective is to determine whether two references refer to the same object, always use the equals(...) method to determine equality of content.

    Incidentally, your code doesn't fulfill the requirement. Which is just as well, as it might otherwise have deprived scurvyjones of a learning opportunity.

    db

    db

  8. #8
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    I think he want to search all the items containing "Tom".
    e.g: "Tomcat", "Tommy",....
    If so, you have to crate a method to recognize if the input is a substring of the selected item.
    e.g: public boolean recognizeItem( String searchKey, String selectedItem )
    { //method body }

    Then you can loop over the array and check if recognizeItem() return true, print out the item.
    e.g:
    Java Code:
    for( int i = 0; i < array.length; i++ ) // assume it is a 1-D array
    {
         if recognizeItem( userInput, array[ i ] )
               System.out.println( array[ i ] );
    }
    Hope it useful :)

Similar Threads

  1. How to create Java Button Link
    By Louis in forum New To Java
    Replies: 3
    Last Post: 11-17-2010, 07:29 AM
  2. Replies: 5
    Last Post: 10-13-2009, 01:35 PM
  3. How to create a Ploygon Button
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:39 PM
  4. search button java applet awt
    By perplexingtrax in forum New To Java
    Replies: 2
    Last Post: 03-24-2008, 01:49 PM
  5. Dynamically create a button, but what happened?
    By love2java in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:01 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
  •