Last active
April 17, 2017 13:03
-
-
Save dflemstr/0401023c10b89f93da19d3b6c94a78d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A list of words. | |
*/ | |
class WordList { | |
int maxCapacity = 1024; | |
String[] words; | |
/** | |
* Load the words | |
*/ | |
public WordList(String fileName) { | |
File file = new File(fileName); | |
InputStream input = new FileInputStream(file); | |
Reader reader = new InputStreamReader(input); | |
BufferedReader bufferedReader = new BufferedReader(reader); | |
String line = null; | |
int i = 0; | |
words = new String[maxCapacity]; | |
while (true) { | |
try { | |
line = bufferedReader.readLine(); | |
if (line == null) { | |
break; | |
} | |
words[i++] = line; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
/** | |
* Check if the word is in the list | |
*/ | |
public boolean contains(String word) { | |
boolean result = false; | |
for (int i = 0; i < maxCapacity; i++) { | |
if (word == words[i]) { | |
result = true; | |
} | |
} | |
return result; | |
} | |
/** | |
* Remove the word from the list | |
*/ | |
public boolean remove(String word) { | |
for (int i = 0; i < maxCapacity; i++) { | |
if (word == words[i]) { | |
words[i] = null; | |
} | |
} | |
} | |
/** | |
* Replace a word with another one | |
*/ | |
public boolean replace(String original, String replacement) { | |
for (int i = 0; i < maxCapacity; i++) { | |
if (original == words[i]) { | |
words[i] = replacement; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment