title: Java ArrayList: check if an id is in the list
body: <p>I am having trouble solving this basic problem in Java. I have an array of 5 animal cages, each cage has an ID.
  When I try to verify if a cage already contains an animal in it, the check works only for the first 2 cages, anything after that is buggy.</p>
  
  <p>this is the main:</p>
  
  <pre><code>    Cat picasso = new Cat ("Cat", "Picasso");
      Cat athena = new Cat("Cat", "Athena");
      Dog thunder = new Dog ("Dog", "Thunder");
      Dog buffy = new Dog("Dog", "Buffy");
  
      Pets store = new Pets("Super store");
  
      store.addToCage(buffy, 1);
      store.addToCage(athena, 1);
  </code></pre>
  
  <p>this throws an error message as it should: "cage is full"</p>
  
  <p>but this one does not:</p>
  
  <pre><code>    store.addToCage(buffy, 1);
      store.addToCage(athena, 2);
      store.addToCage(thunder, 2);
  </code></pre>
  
  <p>It prints the message (cage is full) but it still adds the animal in cage 2. anything more than 2 cages is problematic: it prints the 3rd cage twice for example.</p>
  
  <p>here is the method addToCage in the class Pets:</p>
  
  <pre><code>     class Pets {
  
          ArrayList&lt;Cage&gt; cages = new ArrayList&lt;Cage&gt;();
  
       public void addToCage(Animal animal, int cageId){
         int nbrCages = cages.size();
         Cage tmp, cage;
  
         if(nbrCages == 0){
             cage = new Cage(cageId);
             cage.setAnimal(animal);
             this.cages.add(cage);
         }else{
  
              for(int i=0; i &lt; nbrCages; i++){
                  tmp = cages.get(i);
                  if(tmp.getId() == cageId){
  
                  System.out.println("cage is full");
                  }else{
                      cage = new Cage(cageId);
                      cage.setAnimal(animal);
                      this.cages.add(cage);
  </code></pre>
  
  <p>This is the method that prints the results:</p>
  
  <pre><code>    public String displayResults(){
          String results="";
          int nbrCages = cages.size();
  
          if(nbrCages &gt; 0){
             Cage tmp;
             for(int i=0; i &lt; nbrCages; i++){
               tmp = cages.get(i);
               results += tmp.showCages();
           }
  
         }else{
         results += "store is empty"; 
      }
  
         return results;
     }
  </code></pre>