Skip to content

Instantly share code, notes, and snippets.

@dflemstr
Last active April 17, 2017 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dflemstr/0401023c10b89f93da19d3b6c94a78d0 to your computer and use it in GitHub Desktop.
Save dflemstr/0401023c10b89f93da19d3b6c94a78d0 to your computer and use it in GitHub Desktop.
/**
* 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