Search code examples
javaarraylistiteratortostring

java.util.ConcurrentModificationException Error while trying to use iterator


I am writing a program where I create an ArrayList, and I want to traverse the list with an iterator:

ArrayList<Person> flightAttendants = new ArrayList<Person>();
Iterator<Person> itr = flightAttendants.iterator();

Here is how I am trying to traverse the elements of the arraylist:

I have defined a toString method too:

while(itr.hasNext())
{
    System.out.println(itr.next());
}

public String toString()
{
    System.out.println("name of the passenger : "+name);
    System.out.println("Age of the passenger : "+age);
    System.out.println("Seat number of the passenger : "+seatNumber);
    return "\n";            
}

Whenever I try to run it, it gives me the error: java.util.ConcurrentModificationException

Where is the error here?

Update: here is the full code:

import java.util.*; 
import java.io.*;

class Person
{
    Integer age;
    String name;
    String seatNumber;
    Integer fare;
    int pnr;
    Person()
    {
        try
        {
            BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the name of the passenger");
            name = b.readLine();
            System.out.println("Enter the age of the passenger");
            age = Integer.parseInt(b.readLine());
            System.out.println("Enter the Seat Number you want");
            seatNumber = b.readLine();
            pnr = (int)(Math.random()*100000000);
            System.out.println("PNR number of the passenger is : "+pnr);
        }
        catch(Exception e)
        {
            System.out.println("");         
        }
    }
    public String toString()
    {
        System.out.println("name of the passenger : "+name);
        System.out.println("Age of the passenger : "+age);
        System.out.println("Seat number of the passenger : "+seatNumber);
        return "\n";                
    }
}
class EconomyPassenger extends Person
{

}
class BusinessPassenger extends Person
{

}
class Crew extends Person
{

}
public class Airline
{
    public static void main(String[] args) 
    {
        ArrayList<Person> flightAttendants = new ArrayList<Person>();
        Iterator<Person> itr = flightAttendants.iterator();
        while(true)
        {
            try
            {
                System.out.println("Welcome to Indigo!!!");
                BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter your Choice");
                System.out.println("1.Book Tickets");
                System.out.println("2.Check Reservation");
                System.out.println("3.Update Tickets");
                System.out.println("4.Exit");
                int choice=Integer.parseInt(b.readLine());
                if(choice<0 || choice>4)
                {
                    throw new InvalidChoiceException("Enter a valid choice between 1 and 4");
                }
                switch(choice)
                {
                    case 1: System.out.println("\n\n1.Economy*******2.Business*******3.Crew Login*******4.Exit");
                    // BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
                    int c = Integer.parseInt(b.readLine());
                    if(c==1)
                    {
                        flightAttendants.add(new EconomyPassenger());
                    }
                    else if(c==2)
                    {
                        flightAttendants.add(new BusinessPassenger());
                    }
                    else if(c==3)
                    {
                        flightAttendants.add(new Crew());
                    }
                    else if(c==4)
                    {
                        break;
                    }
                    break;
                    case 2: // System.out.println("Enter your PNR Number : ");
                    // int p = Integer.parseInt(b.readLine());
                    // System.out.println(p);
                    while(itr.hasNext())
                    {
                        System.out.println(itr.next());
                    }
                    break;
                    case 3: System.out.println("case 3");break;
                    case 4: return;
                    default: System.out.println("default");
                }
            }    
            catch(InvalidChoiceException ic)
            {
                // System.out.println(ic);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }
}
class InvalidChoiceException extends Exception
{
    InvalidChoiceException()
    {}
    InvalidChoiceException(String msg)
    {
        System.out.println(msg);
    }
}

Solution

  • The code being provided by you should work fine as I have tried it. Unless you show there is something else your code is working upon we cant further try to solve. I suggest you the check the question Iterators and the concurrentmodificationexception also for better understanding that your code may be somewhere falling into errors mentioned there.

    As mentioned by Andrew bring the iterator into your while loop and thats working fine check now. I tried it.