I tried running the for-loop, but it would not run since allWords was empty. Therefore I added the first if-statement, so allWorlds would have atleast one item, and therefor the loop would run. The loop now runs, but I get:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
A few google searches told me that this happens because I try to change the ArrayList while its in use (?). I wonder if there is an easy fix, or if I have to rewrite the code. Thanks in advance.
The code:
private void addWord(String word){
if(allWords.isEmpty()){ //allWords defined above.
allWords.add(word);
}
for (String check : allWords){
if (!check.equalsIgnoreCase(word)){
allWords.add(word);
}
else if(check.equalsIgnoreCase(word)){
System.out.println("This word is a duplicate.");
}
else{
System.out.println("Something went wrong.");
}
}
}