title: How can I implement loops and if-else logic for ActionListener?
body: <p>I'm trying to build a simple quiz game using Swing.  The user should be able to pick a fruit or vegetable from the combobox and guess if it is a fruit or vegetable.  If they guess correctly, the GUI should output "yes". If they guess wrong, it should output "no".  The code seems to compile correctly, but it is not running correctly.</p>
  
  <p>I am using an if-else loop within the "fruit" and "vegetable" button's ActionListener in order to determine if the user guessed correctly.</p>
  
  <pre><code>public class GUI {
  
      public static String input;
  
      public static void main(String[] args) {
  
          new GUI();
      }
  
      public GUI()
      {
  
  
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setTitle("Simple GUI");
          frame.setSize(400,300);
          frame.setLocationRelativeTo(null);
  
          final String[] fruitOptions = {"Apple", "Apricot", "Banana"
                  ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"};
  
  
  
          final String[] vegOptions = {"Asparagus", "Beans", "Broccoli", "Cabbage"
                  , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom"
                  , "Pepper", "Radish", "Shallot", "Spinach", "Swede"
                  , "Turnip"};
  
          String[] combined = {"Apple", "Apricot", "Banana"
                  ,"Cherry", "Date","Cucumber", "Leek", "Mushroom"
                  , "Pepper", "Radish","Kiwi", "Orange", "Pear", "Strawberry", "Asparagus", "Beans", "Broccoli", "Cabbage"
                  , "Carrot", "Celery","Shallot", "Spinach", "Swede"
                  , "Turnip"};
  
  
  
  
          JPanel comboPanel = new JPanel();
          JLabel comboLabel = new JLabel("Is it a Fruit or Vegetable?:");
          final JComboBox fruitsVeggies = new JComboBox(combined);
  
          ActionListener actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  input = (String) fruitsVeggies.getSelectedItem();
  
              }
          };
  
  
  
          fruitsVeggies.addActionListener(actionListener);
          comboPanel.add(comboLabel);
          comboPanel.add(fruitsVeggies);
  
  
          final JButton fruitButton = new JButton( "Fruit");
          JButton vegButton = new JButton("Vegetable");
  
  
  
          final JLabel yes = new JLabel("YES");
          final JLabel no = new JLabel("NO");
  
  
          ActionListener listener = new ActionListener() 
          {
  
  
  
              public void actionPerformed (ActionEvent d)
              {
                  int i;
  
                  for(i=0; i&lt;vegOptions.length-1; i++) 
                  {
                      if (input.equals(fruitOptions[i])) 
                      {
                           yes.setVisible(true);
                      }
                      else
                          no.setVisible(true);
                  }   
  
  
  
  
              }
          };
  
  
          ActionListener vegListener = new ActionListener() 
          { 
  
  
              public void actionPerformed(ActionEvent f)
              {
                  int i;
  
                  for(i=0; i&lt;vegOptions.length-1; i++) 
                  {
                      if (input.equals(fruitOptions[i])) 
                      {
  
                           no.setVisible(true);
                      }
                      else
  
                          yes.setVisible(true);
                  }
              }
          };
  
          no.setVisible(false);
          yes.setVisible(false);
          fruitButton.addActionListener(listener);
          vegButton.addActionListener(vegListener);
  
          frame.add(comboPanel, BorderLayout.NORTH);
  
          frame.add(vegButton,BorderLayout.WEST);
          frame.add(fruitButton, BorderLayout.EAST);
          frame.add(yes, BorderLayout.CENTER);
          frame.add(no, BorderLayout.CENTER);
  
  
          frame.setVisible(true);
      }
  
  }
  </code></pre>