* This array is then sent to the WriteToFile class, where the dictionary
* file is truncated and rewritten.
*/
public boolean removeWord(String word) {
ReadFile reader = new ReadFile();
WriteToFile writer = new WriteToFile();
ArrayList<String> allWords = new ArrayList<String>();
boolean wordRemoved = false;
String path = "dictionary.txt";
allWords = reader.readFromFile(path);
allWords.removeAll(Collections.singleton(null));
System.out.println("old size: " + allWords.size());
for(int i = 0; i < (allWords.size() - 1); i++) {
if(word.equals(allWords.get(i))) {
allWords.remove(i);
wordRemoved = true;
}
}
allWords.removeAll(Collections.singleton(null));
String[] toBeWritten = new String[allWords.size()];
for (int j = 0; j < allWords.size(); j++) {
System.out.println(allWords.get(j));
toBeWritten[j] = allWords.get(j);
}
System.out.println("tobewritten: " + toBeWritten.length);
System.out.println("New size: " + allWords.size());
for (int k = 0; k < toBeWritten.length; k++) {
System.out.println("toBeWritten: " + toBeWritten[k]);
}
try {
writer.truncateAndWriteFile("dictionary.txt", toBeWritten);
} catch(Exception e) {
System.out.println("Error: " + e);
}