For a java homework assignment we were assigned to initialize an ArrayList of Strings containing dictionary words. If the words were not of a length we generated ,they are then to be removed from the ArrayList. The following code is not working.
public static int randLength() {
int range=(RAND_MAX-RAND_MIN)+1;
int randomNum=(int)(Math.random()*range)+RAND_MIN;
return randomNum;
}
This function works correctly.
int randomLength=randLength();
System.out.println(randomLength);
for(int i=0;i<dictionaryList.size()-1;i++) {
if(dictionaryList.get(i).length()!=randomLength) {
System.out.println("The lucky word is " + dictionaryList.get(i));
dictionaryList.remove(i);
}
}
This however, does not. dictionaryList was initialized using a text file of words. Mine were, "apples" "beer" "charlie" "dogs" "elephant" "fogerty" In some cases, the code above does remove words that are not of the randomLength value. However it does not remove them all. Any help would be appreciated.


i < dictionaryList.size(), Why you minus one here?