View Full Version : Need help with indexing (Java or Obj-C)

Jun 27, 2012, 06:53 PM
Hello all, I'm trying to build a small app in either Java or Obj-C that will keep a list of movies in a .txt file to which I can add to (by means of the app) and then search that list using an index. The part I'm having trouble is with the index, as the adding and deleting methods are pretty straightforward. I was thinking of giving each movie a numeric code (0001, 0002, etc) that would just increase by 1 with each movie and use that code for searching, but I also want to be able to search by typing one or more words from the title.
For example, if I type 'war' in the search field, it should show me 'War of the worlds' and 'World at War' along with their information.
I'm really stumped on this, and while I know how to handle files in Obj-C and Java, I'm not sure how to go about this...

Jun 27, 2012, 08:02 PM
1. Simple way:
Read a line. Does it contain the substring (e.g. "war")? If so, add to results (e.g. an NSMutableArray or a Java List). Repeat until EOF.

2. Faster but still simple-minded way:
Read a line. Add to collection of lines. Repeat until EOF.
To search, search the collection of lines (faster than reading a file every search).

3. Complex way:
Read a line. Break into words. Add words to set (NSSet or Java Set). Add set to collection of sets, e.g. an array or list where the index of the set is its number, e.g. 001.
To search, search the sets (faster than scanning a string every time, because a set can more quickly determine presence/absence). For anything less than 10,000 movies, I doubt the speed difference would be significant.

4. More complex way:
Read a line. Break into words. Add word to dictionary, with word as key and array of indexes as value. If word is already in dictionary, append index to array of values.
To search, use the word as key to dictionary. Value is array of indexes. Each index is the number of the movie whose title contains the word.

Notice how each approach is built from simpler pieces. So it's really just a question of Breaking It Down and figuring out what each individual piece is. If an individual piece is still too complex, then break it down, too.

Jun 27, 2012, 09:09 PM
Wow, I never thought about it like that... Here I was complicating everything... Thanks a lot, I think I can get on the right track now =]

Jun 27, 2012, 09:40 PM
Sounds like a job for SQL!!

Jun 28, 2012, 05:17 PM
Sounds like a job for SQL!!

Sadly, I'm not allowed to use a real database for this exercise

Jul 5, 2012, 05:43 PM
Since you are doing this in Java anyway, do you want to use Lucerne/Solr?