Have written code for a game, a letter is randomly choosen by the programme and then the player has to pick the same one with the sequence getting longer and longer, the choosen letter is added to an arrayList for the game and then when the player picks a letter it is added to the player arrayList, it then to see if the sequence matches and then continues which is working, the only problem im having is i want to print out lines show current sequences size and current sequence, it is working for the computers sequence but it isnt for the player sequence.
instance variable

private ArrayList<Character> playerSequence;

inside the constructor

playerSequence = new ArrayList<Character>();

Code to add the character to the arrayList when that button is clicked

public class ButtonWatcher implements ActionListener
    {
       public void actionPerformed(ActionEvent b)
       {
          Object clicked = b.getActionCommand();
          if(clicked.equals("A"))
          {
            playerSequence.add('A');
            
          }
          if(clicked.equals("B"))
          {
              playerSequence.add('B');
              
          }
          if(clicked.equals("C"))
          {
             playerSequence.add('C');
          }
          if(clicked.equals("D"))
          {
             playerSequence.add('D');
          } 
        
       }
}

I then run a method which prints out the computers sequence and the players sequence and sequence size but the size comes up as 0 and sequence appears as [], so im guessing that the arrayList is clearing after the first round and then starting a fresh for the next sequence when i want it to hold the sequence (does that make sense)

System.out.println("player sequence " + playerSequence);
System.out.println("sequence length" + computerSequence.size());
System.out.println("player length " + playerSequence.size());
public ArrayList<Character> getPlayerSequence()
    {
        return playerSequence;
    }

Any pointers would be great, thanks

Add a println inside where you add to the arraylist, to make sure it is actually adding, print out the list also at this point.

Done that and yes its adding it and printing the size of correctly must be a problem outside the methods ive shown then

This article has been dead for over six months. Start a new discussion instead.