title: concurrentModificationException in ArrayList
body: <p>Problem statement:
  Design a system for the following scenario:</p>
  
  <blockquote>
    <p>1.An item list contains item code, name, rate, and quantity for several items.</p>
    
    <ol start="2">
    <li><p>Whenever a new item is added in the list uniqueness of item code is to be  checked. Register a new product with its price.</p></li>
    <li><p>Time to time rate of the items may change.</p></li>
    <li><p>Whenever an item is issued or received existence of the item is  checked and quantity is updated.</p></li>
    <li><p>In case of issue, availability of quantity is also to be checked.</p></li>
    <li><p>User may also like to know price/quantity available for an item.</p></li>
    <li><p>Find how many items cost more than a given amount. The amount will be a  parameter.</p></li>
    <li><p>Remember that the methods have to return an error code if for example an invalid  item code is given</p></li>
    </ol>
  </blockquote>
  
  <p>For the above problem i have created a <strong><em>Item</em></strong> class with following members:</p>
  
  <pre><code>private String name;
  private double rate;
  private long code;
  private int quantity;
  
  public Item()
  public Item(String name, double rate, long code, int quantity)
  public Item(Item item) 
  public String toString()
  public String getName() 
  public void setName(String name) 
  public double getRate()     
  public void setRate(double rate) 
  public long getCode()
  public void setCode(long code) 
  public int getQuantity()
  public void setQuantity(int quantity)
  </code></pre>
  
  <p>Now I have created a <strong><em>Shop class</em></strong> to access the <strong><em>Item class</em></strong>, to do all operations on 
  a Item... Here is a part of the shop class.</p>
  
  <pre><code>    private ArrayList&lt;Item&gt; ItemList;
      private Iterator&lt;Item&gt; itr;
  
      public Shop() {
          System.out.println("New Shop for Items created.");
          ItemList = new ArrayList&lt;Item&gt;();
          itr= ItemList.iterator();
      }
  
      public Item search(long code) {
          Item item;
          while(itr.hasNext()) {
              item = new Item(itr.next());
              if (item.getCode() == code) {
                  return item;
              }
          }
          return null;
      }
  
      public void addItem() throws InputMisMatchEXception{
          long aCode;
          String aName;
          double aRate;
          int aQuantity;
          Item foundItem;
  
              System.out.println("Enter Item code:");
              aCode = sc.nextLong();
              foundItem = search(aCode);
              if (foundItem == null) {
                  System.out.println("Item name : ");
                  aName = sc.next();
                  System.out.println("Rate : ");
                  aRate = sc.nextDouble();
                  System.out.println("Quantity : ");
                  aQuantity = sc.nextInt();
                  Item aItem = new Item(aName, aRate, aCode, aQuantity);
                  ItemList.add(aItem);
              } else if (foundItem != null) {
                  System.out.println("Item exists");
              }
      }
  </code></pre>
  
  <p>Now when i am adding a new item, it works.. But When I add a second element it throws a </p>
  
  <blockquote>
    <p>ConcurrentModificationException  </p>
  </blockquote>
  
  <p>Here is a sample output:<br>
  New Shop for Items created.<br>
  *</p>
  
  <blockquote>
    <p>-----ITEM------<br>
    1. Add items to list<br>
    2. Update item list<br>
    3. Issue item<br>
    4. Display item details<br>
    5. Exit<br>
       Choice:  1<br>
    Enter Item code: 123<br>
    Item name : qwerty<br>
    Rate :  12345<br>
    Quantity :  100<br>
    1. Add items to list<br>
    2. Update item list<br>
    3. Issue item<br>
    4. Display item details<br>
    5. Exit<br>
       Choice: 1<br>
    Enter Item code: 1212<br>
    Exception in thread "main"<br>
    java.util.ConcurrentModificationException     at<br>
    java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)    at<br>
    java.util.ArrayList$Itr.next(ArrayList.java:851)  at<br>
    items.Shop.search(Shop.java:28)   at items.Shop.addItem(Shop.java:55)<br>
      at items.ItemDemo.main(ItemDemo.java:26)  </p>
  </blockquote>
  
  <p>What am i doing wrong?
  (Tried to give all relevant classes)</p>