I need some help with an iterator that it seems no matter what I do it keeps giving me the error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at package.Dictionary.writer(Dictionary.java:72)
at package.main.main(main.java:24) <5 internal calls>
I could use any help given to help solve this, I am somewhat new to Java programming. My full code is below:
package package;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class Dictionary {
Collection<String> webster = new ArrayList<String>();
Iterator<String> iter = webster.iterator();
File path = null;
public Dictionary(Collection<String> words) {
if (words == null) {
throw new NullPointerException("Error: Collection NULL");
} else {
if (!words.isEmpty()) {
clear();
}
}
}
public long load(File file) throws FileNotFoundException {
String filePath = file.getAbsolutePath();
if (file.getAbsolutePath().equals(null)
|| file.getAbsolutePath().equals("")) {
throw new FileNotFoundException("Error: No File Found!");
} else {
if (file.exists()) {
Scanner fileScanner = new Scanner(new File(filePath));
long time = System.nanoTime();
while (fileScanner.hasNext()) {
webster.add(fileScanner.next());
}
long time2 = System.nanoTime();
long duration = time2 - time;
return duration;
} else {
throw new FileNotFoundException("Error: No File Exsists!");
}
}
}
public boolean contains(String target) {
if (webster.contains(target)) {
return true;
} else {
return false;
}
}
public void clear() {
webster.clear();
}
public void writer() throws Exception {
PrintWriter out = new PrintWriter("words.txt");
while (iter.hasNext()) {
out.println(iter.next());
}
out.close();
}
}