Using ArrayList to store Lines in a text File

Hey Guys,

I have the following program that I am using to read a text file and store each word in each line in an ArrayList. But I am getting the following error;

Exception in thread"main" java.util.ConcurrentModificationException

at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)

at java.util.AbstractList$Itr.next(AbstractList.java:420)

at bus.OrderReader.parseLine(OrderReader.java:50)

at bus.OrderReader.main(OrderReader.java:32)

The code is as follows;

package bus;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Scanner;

import java.util.Collection;

import java.util.ArrayList;

import java.util.Iterator;

publicclass OrderReader{

static Collection bread =new ArrayList();

static Collection filling =new ArrayList();

static Iterator iter1 = filling.iterator();

static Iterator iter2 = bread.iterator();

privatestaticint counter;

publicstaticvoid main(String args[]){

Scanner s =null;

try{

s =new Scanner(new BufferedReader(new FileReader("/Users/admin/Documents/workspace2/OBS/WebContent/WriteMe.txt")));

s.useDelimiter("\r");

while(s.hasNext()){

parseLine(s.next());

counter++;

}

s.close();

}

catch(FileNotFoundException e){

System.out.println("cannot find the file");

//ignore for now

}

}

publicstaticvoid parseLine(String line){

Scanner lineScanner =new Scanner(line);

lineScanner.useDelimiter("#");

bread.add(lineScanner.next());

filling.add(lineScanner.next());

System.out.println(iter1.next());

System.out.println(iter2.next());

}

}

I do not understand why this does not work. Can someone please point me in the right direction?

Thank You

Jaz

[3521 byte] By [DontKnowJacka] at [2007-11-26 15:50:20]
# 1
You aren't allowed to modify a Collection while you are iterating over it. You are creating iterators as attributes, and then you modify the Collections. Kaj
kajbja at 2007-7-8 22:10:04 > top of Java-index,Java Essentials,New To Java...
# 2

> You aren't allowed to modify a Collection while you

> are iterating over it.

>

> You are creating iterators as attributes, and then

> you modify the Collections.

>

> Kaj

So once I create an iterator as an attribute I can in no circumstance add an object to the list?

Also would I be able to display specific elements of an Arraylist if so how? or would I have to show the entire contents all at once?

Message was edited by:

DontKnowJack

DontKnowJacka at 2007-7-8 22:10:04 > top of Java-index,Java Essentials,New To Java...
# 3

> So once I create an iterator as an attribute I can in

> no circumstance add an object to the list?

No, you can add elements to the collection, but you can no longer use the iterator in that case (create a new iterator)

> Also would I be able to display specific elements of

> an Arraylist if so how?

Call get(int index) on the list that you have

Kaj

kajbja at 2007-7-8 22:10:04 > top of Java-index,Java Essentials,New To Java...
# 4

Instead of doing this:

> bread.add(lineScanner.next());

> filling.add(lineScanner.next());

> System.out.println(iter1.next());

> System.out.println(iter2.next());

where presumably you're just printing what you just barely added, do this:

Object breadObject = lineScanner.next();

Object fillingObject = lineScanner.next();

bread.add(breadObject);

filling.add(fillingObject);

System.out.println(breadObject);

System.out.println(fillingObject);

And get rid of the now unused iterators.

warnerjaa at 2007-7-8 22:10:04 > top of Java-index,Java Essentials,New To Java...
# 5

> Instead of doing this:

> > bread.add(lineScanner.next());

> > filling.add(lineScanner.next());

> > System.out.println(iter1.next());

> > System.out.println(iter2.next());

>

> where presumably you're just printing what you just

> barely added, do this:

> Object breadObject = lineScanner.next();

> Object fillingObject = lineScanner.next();

> bread.add(breadObject);

> filling.add(fillingObject);

> System.out.println(breadObject);

> System.out.println(fillingObject);

>

> And get rid of the now unused iterators.

Im not sure I understand quiet what is happening here. Are you creating a primitive data type of type Object and then storing all the values in that variable?

why would I do this when this only allows me to view the very last element that was added to the list? I want to view the entire list.

Thanks

Jaz

Message was edited by:

DontKnowJack

DontKnowJacka at 2007-7-8 22:10:04 > top of Java-index,Java Essentials,New To Java...