Apr 012013
 

How to read words from a file into your Titanium application to make a dictionary that can be checked against.This plug and play sample shows you how to read words from a file into your Titanium app line by line, then use a function to see if the word of your choice exists in the list.

  1. // Array for word list
  2. var wordList = []
  3.  
  4. // Get the wordlist.txt file from the Resources directory
  5. var readFile = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, "wordlist.txt");
  6.  
  7. // If the file exists
  8. if (readFile.exists()){
  9. // Split the text file by line
  10. var text = readFile.read().text.split('n');
  11.  
  12. // Add the words, by line, into the wordList array
  13. for(var i=0;i<text.length;i++)
  14. {
  15. wordList[i] = text[i]
  16. }
  17. }
  18. // If the file doesn't exist print "no file found"
  19. else
  20. {
  21. Ti.API.info("no file found")
  22. }
  23.  
  24. // Simple dictionary check
  25. checkIt = function(currentWord){
  26. for (var i=0;i<wordList.length;i++)
  27. {
  28. if (currentWord === wordList[i])
  29. {
  30. Ti.API.info("Word is correct!")
  31. break
  32. }
  33. else if(i==wordList.length-1)
  34. {
  35. Ti.API.info("Word is incorrect!")
  36. }
  37. }
  38. };
  39.  
  40. // Try checking a word
  41. checkIt("PEACH")

Wordlist.txt was a file I grabbed off the official Scrabble website and used in my Boggle Clone last weekend, which I created using Lanica’s Platino game engine.

Simply paste this code into your app.js file and ensure you have wordlist.txt or an alternative in your Resources directory and you’re good to go.

Although this is fairly simply I hadn’t done it before with javascript and had a very hard time finding an intuitive method, so I thought it was worth sharing the end results here.

To make file loading, and saving, even easier in Titanium (and soon, in Platino) please stay tuned as sometime in the next week I hope to share Ego with you.

Ego started as a simple library for another SDK to allow the saving and loading of files simply by using saveFile(“value or string here”, “filename.txt”) and loadFile(“filename.txt”) – although the code to do this is not overly complicated it’s a useful thing to have and I look forward to posting it shortly.

Happy Easter Everyone!

Peach Pellen – @peachpellen

 

Like this post? Subscribe to the monthly newsletter!

 

  8 Responses to “Read Words from a File into your Titanium Application”

  1. […] or having produced for me within the 45-day limit of the trial. By chance I was looking at Peach Pellen’s Techority site and a post about reading a word list into a Titanium application. Platino sits on top of […]

  2. Peach….seriously, I need to know if I should stay where I am or move over. This is a real predicament, I feel that the other side has lost it now and there is no support to speak of, I am loathe to renew my subscription….at the same time, will I be able to learn all of this new stuff? I do love you and Carlos :)

    • Hey Joanne :-)

      I am sorry to hear about the lack of support, that’s a shame. What I recommend really depends on what you want to do and where you are at with developing apps right now. Is it a hobby? Are you making a career out of it? Games? Books? Utilities? etc.

      Yes, you’ll be able to learn “this new stuff” – I’ve already started writing samples for when we leave beta to help ease new users into how things work. But that doesn’t mean Platino is for you; it may be overkill.

      Write me an email and lay out for me what you want to do; what kind of apps you’re making right now, what kind you plan to make, if it’s still a bit of a hobby or something you’re actively working at, and we will go from there.

      Thanks for the kind words about myself and Carlos, most appreciated :-)

  3. Congrats on your new position Peach.
    I think this will be way over my head!
    Anyway, I shall continue to follow your techority site, as you gave me hope when I knew nothing at all. I am still a beginner, but I shall watch you rise above them all.

    GL

  4. A very nice idea. I would simplify the code though a little.
    The “text” var obtained by splitting the content of the file by newline is already an array so the next for loop that simply duplicates it to wordList is kind of useless.

    Just my 2c :)

    • Thanks Dan – great advice! I’m still very new to JavaScript and haven’t coded a whole lot in the last year. Will review this over the weekened :)

    • The loop is only useless because we happen to want an array with every line in the file, verbatim. If this file were a CSV we could use this loop to do some additional parsing, e.g. if each line were something like PEACH,100 we could split it by comma, where the 0th cell of the resulting array is the word and the 1st cell is the score for spelling that word (since Peach is worth 100 points, clearly)

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.