0

I want to loop through an arraylist to see wheather the p.name(Player name) is equal to what ever the user type in a text-box; when I enter a value it work for the first name but when I change the value in the text-box it throw a concurrent modification error.

Company class

 public class Company {

  int shares = 100;
  ArrayList <Player> shareHolders = new ArrayList<>();

  if (condition()) {
    for (Player p: company.shareHolders) { // error highlight this line
            if (p.name.equalsIgnoreCase(txtname.getText()) && 
                    (company.shares <= 100 && company.shares >0)) {

                    company.shares -= Integer.parseInt(txtShares.getText());
                    p.shares += Integer.parseInt(txtShares.getText());
                    System.out.println(p.name+":"+p.shares+" | Company"+company.shares);
                    return;
                }else{
                    company.shareHolders.add(new Player(txtname.getText()));
                    System.out.println(txtname.getText()+" added");
                }
            }
        }
Thiyagu
  • 17,362
  • 5
  • 42
  • 79

1 Answers1

0

You are modifying company.shareHolders during an iteration over it, which is not allowed (hence the ConcurrentModificationException).

alephtwo
  • 322
  • 2
  • 10