-1

I'm creating a simple spellchecking program. It basically loads in a dictionary and misspelled words. I have a function aimed at removing repeats, and another function that generates all vowel combinations of a word to be checked.

I'm having an issue with my vowel function. I keep getting CurrentModifcationException, but I feel like it should be able to modify the array its adding words to as the array is statically defined at the top.

Basically this function takes in an ArrayList and recursively adds additional words to the ArrayList which are vowel combinations of the word inputted. An example is if 'who' was inputted, it would add 'wha, whi, whe, whu' to the array list.

The Code

DEFINED AT TOP OF CONTAINER CLASS

private static List<String> vowelList = new ArrayList<>();
private static ArrayList<String> checkList = new ArrayList<>();

private static void feedWordsForVowelCheck() {

        for(String temp : checkList) {
            System.out.println("Checking vowels for: " + temp);
          //  createVowelCombosStatic(temp, 0);
            createVowelCombos(temp, 0 , checkList);
        }

       // System.out.println("Does this finish?");
    }

  private static void createVowelCombos(String word, int start, ArrayList<String> checkList) {
        StringBuilder sbAddWord = new StringBuilder(word);
        String[] splitWord = word.split("");

        
        if (start==splitWord.length) {
            checkList.add(word);
            return;
        }
        
        if (splitWord[start].matches(".*[aeiou]")) {
            for (int j = 0; j < 5; j++) {
                sbAddWord.setCharAt(start, vowelList.get(j).charAt(0));
                createVowelCombos(sbAddWord.toString(),start+1, checkList);
                //System.out.println(sbAddWord.toString());
            }
        }
        else
            createVowelCombos(sbAddWord.toString(),start+1, checkList);
    }
Community
  • 1
  • 1
user3622460
  • 1,231
  • 5
  • 23
  • 42
  • Is it concurrent modification exception? You are seeing this error, because you are modifying checkList in createVowelCombos while looping through it, Please use Iterator instead – Yohannes Gebremariam Feb 27 '17 at 21:27
  • Your error comes from somewhere else, is it possible that you launched several threads? – Adonis Feb 27 '17 at 21:28
  • Stacktrace, please? – OneCricketeer Feb 27 '17 at 21:29
  • you get concurrent modification exception, for example: `List someList = new ArrayList<>(); //assume you have filled the list with some data for(String s: someList) { someList.remove(x) //some string in this list }` I am unsure what your createVowelCombos method does. But, I am confident that this method is causing this exception because I can see you are passing the checkList, which will be passed as reference. If I were you I will go back and check this method. – Anil Pediredla Feb 27 '17 at 21:30

2 Answers2

3

You cannot modify a list you are iterating over. This will give you a concurrent modification.

You are iterating here:

 for(String temp : checkList) {

but you are also adding values here:

 if (start==splitWord.length) {
            checkList.add(word);
            return;
        }

checkList is the same list you are iterating over. If you need to add values while you are iterating look into ListIterator

Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
1

Is it concurrent modification exception? You are seeing this error, because you are modifying checkList in createVowelCombos while looping through it, Please use Iterator instead

write something like this :

Iterator itr = checkList .iterator();

    while(litr.hasNext()) {
        System.out.println("Checking vowels for: " + temp);
      //  createVowelCombosStatic(temp, 0);
        createVowelCombos(temp, 0 , checkList);
    }

   // System.out.println("Does this finish?");
}

Moreover, your logic looks to have some issue. You can't call a method within itself recursively without a means(rule) that ends it. Yours will run forever

Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23