Results 1 to 8 of 8
Thread: Create a Search Button - Java
- 12-07-2010, 03:10 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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
-
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!
- 12-07-2010, 03:28 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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
- 12-07-2010, 06:59 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try anything on your problem? How long you are working with Java and related stuff?
- 12-07-2010, 10:55 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 27
- Rep Power
- 0
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.
- 12-08-2010, 09:26 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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
- 12-08-2010, 10:09 AM #7
@gayankan, learn to use the code tags for posting code.
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.or intead of == you can use if( ar.get(i).equals( "X")) as well
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
- 12-08-2010, 12:40 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
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:
Hope it useful :)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 ] ); }
Similar Threads
-
How to create Java Button Link
By Louis in forum New To JavaReplies: 3Last Post: 11-17-2010, 07:29 AM -
Create code for Binary Search Tree using compareTo method using I/O
By harvin23 in forum New To JavaReplies: 5Last Post: 10-13-2009, 01:35 PM -
How to create a Ploygon Button
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:39 PM -
search button java applet awt
By perplexingtrax in forum New To JavaReplies: 2Last Post: 03-24-2008, 01:49 PM -
Dynamically create a button, but what happened?
By love2java in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 12:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks