-1

As usual Arrays and their lists will be the death of me, only this time the lab is working just fine up until the End and then i get a strong error, and me being a java newbie have know idea what this is. hopefully some of you better programmers will be able to help me.

Error + current output.

word with 2 vowels = 5
word with 3 vowels = 0
word with 4 vowels = 1
word with 2 chars = 0
word with 3 chars = 3
word with 4 chars = 2
word with 5 chars = 2
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at Words.removeWordsWithXChars(Words.java:54)
at Lab16c.main(Lab16c.java:25)

output i want

[one, two, three, four, five, six, seven, alligator]
word with 2 vowels = 5
word with 3 vowels = 0
word with 4 vowels = 1
word with 2 chars = 0
word with 3 chars = 3
word with 4 chars = 2
word with 5 chars = 2

after removing words with 3 chars 
[three, four, five, seven, alligator]

number of vowels in the words removed == 4

code in main

public class Lab16c
{
public static void main( String args[] )
{
    Words test = new Words("one two three four five six seven alligator");
    out.println(test);
    out.println("word with 2 vowels = "+test.countWordsWithXVowels(2));
    out.println("word with 3 vowels = "+test.countWordsWithXVowels(3));
    out.println("word with 4 vowels = "+test.countWordsWithXVowels(4));
    out.println("word with 2 chars = "+test.countWordsWithXChars(2));
    out.println("word with 3 chars = "+test.countWordsWithXChars(3));       
    out.println("word with 4 chars = "+test.countWordsWithXChars(4));
    out.println("word with 5 chars = "+test.countWordsWithXChars(5));
    int vowelsRemoved = test.removeWordsWithXChars(3);
    out.println("\nafter removing words with 3 chars \n" + test);
    out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
    out.println("\n\n");        


    //more test cases

}
}

code in third class public int countWordsWithXChars(int size) { int count=0; for(Word i : words) { if(i.getLength() == size) { count++; } } return count; }

//this method will remove all words with a specified size / length
//this method will also return the sum of the vowels in all words removed
public int removeWordsWithXChars(int size)
{
    for(Word i : words)
    {
        if(i.getLength() == size)
        {
            words.remove(i);
        }
    }
    return 0;
}

public int countWordsWithXVowels(int numVowels)
{
    int count=0;
    for(Word i: words)
    {
        if(i.getNumVowels() == numVowels)
        {
            count++;
        }
    }
    return count;
}
public String toString()
{
   return "";
}
}

hopefully you can find my problem , thanks in the future

Super deamon
  • 36
  • 1
  • 10
  • That's quite a bit of code you have posted. I suggest you post your questions in SSCCE form: http://sscce.org. – EJK Dec 17 '14 at 04:22
  • @Superdeamon you can't modify a collection while iterating over the same. Go through the link provided by Matt and the check your stacktrace, you'll get it. – dShringi Dec 17 '14 at 04:44
  • @dShringi see i looked at matt's link and thought the iteration.remove method makes sense i just don't know how to implement that into my char remove class – Super deamon Dec 17 '14 at 04:47

2 Answers2

2

Use the Iterater of the collection to iterate through the elements, and remove the elements using the iterator.

public int removeWordsWithXChars(int size)
{

    Iterator<String> iterator = words.iterator();
    while(iterator.hasNext())
    {
        String word = iterator.next();
        if(word.length() == size)
        {
            iterator.remove(word);
        }
    }
    return 0;
}

Hope this helps.

tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
2

You cannot remove items from a list from within a for each loop in java.

Consider using an indexed for loop, but then you would need to keep track of changed indexes from deletion.

I suggest storing the items you want to delete in a list. Roughly like so:

ArrayList<Word> wordsToRemove = new ArrayList<>();
for(Word word : words)
    wordsToRemove.add(word);
for(Word word : wordsToRemove)
    words.remove(words.indexOf(word));
wordsToRemove.clear();
MeetTitan
  • 3,383
  • 1
  • 13
  • 26