error Help

Hi. I keep getting java.util.ConcurrentModificationException and I am not sure why? Any help? Also I need the numbers to print in columns. Ex:

PrimesNonePowersComposite

208 14

31

import java.util.*;

import java.lang.*;

publicclass fibonacci{

publicstaticvoid main (String[] args){

int newNumb, counter=4,oldNumb=1,olderNumb=0,numbCount=0;

LinkedList<Integer> fibNumb=new LinkedList<Integer>();

LinkedList<Integer> noneNumb=new LinkedList<Integer>();

LinkedList<Integer> primeNumb=new LinkedList<Integer>();

LinkedList<Integer> powerNumb=new LinkedList<Integer>();

LinkedList<Integer> pFactors=new LinkedList<Integer>();

ListIterator<Integer>fibItr=fibNumb.listIterator();

Set factors =new HashSet();

while (numbCount<28){

newNumb=oldNumb+olderNumb;

fibNumb.add(newNumb);

olderNumb=oldNumb;

oldNumb=newNumb;

numbCount++;

}//End While

int first=0;

int second=1;

noneNumb.add(first);

noneNumb.add(second);

//Add numbers that don't fit prime, composite, or power

while (fibItr.hasNext()){

int number=fibItr.next();

if (number<2){

noneNumb.add(number);

}

else{

for (int i = 2; i <= number / i; i++){

while (number % i == 0){

factors.add(number);

number = number / i;

}

}

// if biggest factor occurs only once, number > 1

if (number > 1) primeNumb.add(number);

int factorSize=factors.size();

if (factorSize==0){

powerNumb.add(number);

}

}//End Else

}//End While

System.out.println(noneNumb);

System.out.println(primeNumb);

System.out.println(powerNumb);

}//End Main

}//End Class

[3477 byte] By [Fenris54a] at [2007-10-3 7:44:25]
# 1
show us the stack trace
paulcwa at 2007-7-15 2:45:39 > top of Java-index,Java Essentials,Java Programming...
# 2

I didn't look at you code but you get that error if you try to change a list at the same time as you are searching through it.

For example say you have a list of names (Pete, Paul, John, George) and are Searching for Ringo. If you got to the end your result would be not found but say at the same time something else change Pete to Ringo your result would be incorrect. Therefore you are not allowed to do this.

floundera at 2007-7-15 2:45:39 > top of Java-index,Java Essentials,Java Programming...
# 3

Exception in thread "main" java.util.ConcurrentModificationException

at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)

at java.util.LinkedList$ListItr.next(Unknown Source)

at fibonacci.main(fibonacci.java:37)

I think this is what you mean by track trace....

Fenris54a at 2007-7-15 2:45:39 > top of Java-index,Java Essentials,Java Programming...
# 4
At a glance it looks like you need to get your Iterator *after* you callfibNumb.add(newNumb);
bckrispia at 2007-7-15 2:45:39 > top of Java-index,Java Essentials,Java Programming...
# 5
Oh my gosh. That totally fixed the problem! You guys are life savers! Thank you.
Fenris54a at 2007-7-15 2:45:39 > top of Java-index,Java Essentials,Java Programming...
# 6
now the important question: do you understand why? otherwise we will see you again soon.
dmbdmba at 2007-7-15 2:45:39 > top of Java-index,Java Essentials,Java Programming...