Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iVikD

macrumors regular
Original poster
Sep 11, 2011
227
11
Spain
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...
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,415
A sea of green
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.
 

iVikD

macrumors regular
Original poster
Sep 11, 2011
227
11
Spain
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 =]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.