Skip to content

Instantly share code, notes, and snippets.

@ttreitlinger
Created December 16, 2010 13:17
Show Gist options
  • Save ttreitlinger/743382 to your computer and use it in GitHub Desktop.
Save ttreitlinger/743382 to your computer and use it in GitHub Desktop.
should be part of DictionaryEntry.java
/**
* Lookup a word in the dictionary file
*
* @param word - the word we are looking for
* @return a string array with the word, the translation and description
* or null if the word can't be found
* @throws IOException
*/
public static String[] lookupWord(String word) throws IOException {
List<String> lines = readFileFromDisk(new File("dictionary.txt"));
String[] words = null;
/*
* iterate over all lines
* stop if we find a line which begins with the word we're looking for
*/
for (String thisLine : lines){
if (thisLine.startsWith(word)){
words = thisLine.split(",");
break;
}
}
return words;
}
/**
* Read a text file from disk and return it as a list of strings.
* @param filename
* @return list of strings, each string is one line from the file
* @throws IOException
*/
private static List<String> readFileFromDisk(File filename) throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader inputStream = new BufferedReader(new FileReader(filename));
String line;
while ((line = inputStream.readLine()) != null) {
lines.add(line);
}
return lines;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment