title: Return value of statement in if in a for each loop
body: <p>I have this method which uses a for each loop with an if statement in it</p>
  <pre><code>public static Apartment getApartment(String aNumber)
      
  //for loop to iterate through the list of apartments         
  {   
  for (Apartment x : listOfApartments) 
    { 
    // Variable to hold the value of returned apartment    
    if (x.getApartmentNo().equalsIgnoreCase(aNumber))
    { Apartment chosen = x;
    }
    else 
    {JOptionPane.showMessageDialog(null, &quot;Apartment not found&quot;);
    }
   
  }  
  return chosen;
  } 
  </code></pre>
  <p>I am getting an error that Symbol chosen not found. I think I have declared the variable in wrong place.
  Any help?
  Note: This is a method that takes a String and returns an Object Apartment.</p>