I'm making a program in Java which users can login and upload data to certain people in the program. But, I want to make a search bar so someone can type 'Joe' and it would show anyone with the name 'Joe' in a file. Each name that is registered will be made a file so 'Joe Rico' would have a folder named 'Joe Rico'. I plan to use AWT for the GUI system if that matters and this will be a Java applet if that matters also. (The file system will be stored on the web-server and certain users will have certain permissions). Thanks for the help!
-
I have tried using arrays and such. Just wondering if there's a better way. – StackOverload Nov 08 '13 at 20:54
-
Well. I can't at the moment. I'm at school eating lunch. – StackOverload Nov 08 '13 at 20:55
-
take a box and label it search :P – R.Moeller Nov 08 '13 at 20:57
-
1Well.... I guess I didn't think this through :P – StackOverload Nov 08 '13 at 20:58
-
Sad.. Already got -2 votes – StackOverload Nov 08 '13 at 20:58
-
http://stackoverflow.com/questions/5187888/java-searching-within-a-list-of-objects – Compass Nov 08 '13 at 21:00
-
Thanks! I guess that's why I got the -votes.. :P I'll make sure to search better. – StackOverload Nov 08 '13 at 21:02
-
1Avoid -1s by providing some code that shows what you've been trying and why it doesn't work, etc. Your question is pretty general and would require much information to answer properly as a search box has many elements. Are you asking how to place a text box in a window, how to parse the string input, how to search a file, how to make the record, or show the results? Don't use AWT. – ChiefTwoPencils Nov 08 '13 at 21:05
1 Answers
There's a lot of issues here, and your question is very vague - you could be asking any of the following questions:
How do I make a search box?
You're asking how to properly implement a search box in AWT. First off, use Swing, it's a lot nicer. From there, resources like How to use Text Fields will be very helpful.
How do I make a search application?
You're asking how to implement searching behavior in a GUI. In this case, the key concept you need to understand is Model View Controller, which will let you compartmentalize your business logic from your graphical interface in a clean and efficient way.
How do I efficiently search data?
You're asking for data structures that are good for searching. The classic answer is a Binary Search Tree or a binary search algorithm, like
Collections.binarySearch()
, which allows you to search inO(log n)
time. This may or may not be enough for your use case, but another possible option is a Trie, which provides fast prefix-searching. If these basic data structures aren't good enough for your use case, read on.How do I store data for efficient searches?
Very much dependent on exactly what you're trying to do, but the concept is called Serialization. Java provides serialization behavior natively, however I personally vastly prefer using JSON, and Google's awesome Gson library makes this quite painless.
What tools exist to provide search behavior for me?
If answer #3 wasn't good enough, you're looking for something more powerful. You've got two basic choices, depending on your needs. Either a Relational Database, or a Search/Index library. There are countless database options out there, but I like H2 for quick Java work. A proper search tool like Lucene is almost certainly overkill for your needs, but the option exists.
I hope some of this answers your question. In the future, try to create questions that can have a concrete answer. Questions that are open-ended and leave the notion of "answered" undefined are very difficult to properly address.

- 47,227
- 18
- 148
- 244