dimanche 31 mai 2015

Why is my if/else statement always skipping to the else

I have tried reading this every way I can think of and for the life of me I can't figure out why the if/else statement in the actionlistener will never evaluate the if statement to true. This program is meant to open a JFrame with 3 panels. 2 of them have 8 Random buttons with letters on them. When a button is clicked it is supposed to say if it's a vowel or a consonant. However my program always evaluates the button to a consonant. Where am I not seeing the logical flow of info in the program?

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;


public class JVowelConsenant extends JFrame {

private JPanel contentPane;
ArrayList list = new ArrayList(0);



/**
 * Launch the application.
 */
public static void main(String[] args) {

            try {
                JVowelConsenant frame = new JVowelConsenant();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


/**
 * Create the frame.
 */
public JVowelConsenant() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 585, 371);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

    JPanel panel = new JPanel();
    contentPane.add(panel);
    panel.setLayout(new GridLayout(2, 2, 0, 0));

    JPanel panel_1 = new JPanel();
    contentPane.add(panel_1);
    panel_1.setLayout(new GridLayout(2, 2, 0, 0));

    JPanel panel_2 = new JPanel();
    contentPane.add(panel_2);
    panel_2.setLayout(new GridLayout(2, 2, 0, 0));

    String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    JButton[] letters = new JButton[26];
    for(int i = 0; i<8; i++){

        random_Letters();
        }



     for( int i = 0; i<26; ++i){
        letters[i] = new JButton();
        letters[i].setText(alphabet[i]);


        letters[i].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) { 
                    if(arg0.getSource().equals("A")||arg0.getSource().equals("E")|| arg0.getSource().equals("I")||arg0.getSource().equals("O")||arg0.getSource().equals("U")){
                        JLabel letter_identity = new JLabel("This letter is a Vowel");
                        panel_2.removeAll();
                        panel_2.add(letter_identity);
                        panel_2.revalidate();
                    }
                    else{
                        JLabel letter_identity = new JLabel("This letter is a Consenant");
                        panel_2.removeAll();
                        panel_2.add(letter_identity);
                        panel_2.revalidate();
                    }

            }
        });
    }


    for(int i = 0; i<4;++i){
    panel.add(letters[(int) list.get(i)]);
    panel_1.add(letters[(int) list.get(i+4)]);
    }

}

void random_Letters(){
    Random random = new Random();
    int random_1 = random.nextInt(26);
    int check_point =exclusion(random_1);
    while(check_point == 0){
        random_1= random.nextInt(26);
        check_point = exclusion(random_1);
    }
}

int exclusion(int x){
    int marker = 0;
    if(!list.contains(x)){
        list.add(x);
        marker = 1;
    }
    return marker;


}

}

Aucun commentaire:

Enregistrer un commentaire