I do not work the if else in java swing jbutton

0

Good thing I'm doing a program with java swing. It involves loading in a 266 arraylist questions from a file.txt. In another arraylist I have the 266 answer that I also load it with another file.txt.

I have a textfield that the user must enter the answer when clicking on the button an if else compares the value entered in the textfield with the position of the arraylist the position goes forward with a counter that every time you click on the button the counter sum 1. If the value of the textfield is not the same as the position of the arraylist, a message "Error wrong answer" appears

The problem I have is that even if you enter a value equal to the position of the arraylist, it always enters the else, that is, the values are never the same, although clearly if they are, I have even put two labels on the panel to show what was entered in the textfield with the position of the arraylist at that time.

The funny thing is that if I load an Arraylist manually it can also be seen in the code that I attached the button with the if else if it compares well the two values textfield and position arraylist.

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

public class question_answer extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JButton btnAnswer;
    private JLabel lblNewLabelcuestion;
    private JLabel LabelTextF;
    private JLabel labelArraylistPos;
    private JLabel lblNewLabelTF;
    private JLabel lblNewLabelAL;
    static int count = 0;

    private JLabel lblNewLabel_1;
    private static JTextArea textArea;
    private JLabel lblNewLabel;

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

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

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

        });

    }

    /**
     * Create the frame.
     */
    public question_answer() {
        File archive = null;
        FileReader fr = null;

        //This works
        /*
         * ArrayList<String> question = new ArrayList<String>();
         * question.add("What is your name?");
         * question.add("What is your surname?");
         * question.add("What is your age?");
         * ArrayList<String> answer = new ArrayList<String>();
         * answer.add("Pedro"); 
         * answer.add("Rodriguez");
         * answer.add("46");
         */

        ArrayList<String> answer1 = new ArrayList<String>();

        try {
            archive = new File("C:\answer.txt");
            String line;
            fr = new FileReader(archive);
            BufferedReader br = new BufferedReader(fr);

            while ((line = br.readLine()) != null) {
                answer1.add(line);
            }

            br.close();
        } catch (IOException e) {
            System.out.println(e);
        }

        finally {
            try {
                if (fr != null) {
                    fr.close();

                }
            } catch (IOException e) {
            }
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 567, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        lblNewLabelcuestion = new JLabel("New label");
        lblNewLabelcuestion.setBounds(60, 56, 360, 24);
        contentPane.add(lblNewLabelcuestion);

        lblNewLabelTF = new JLabel("New label");
        lblNewLabelTF.setBounds(60, 292, 56, 16);
        contentPane.add(lblNewLabelTF);

        LabelTextF = new JLabel("TextField");
        LabelTextF.setBounds(60, 263, 56, 16);
        contentPane.add(LabelTextF);

        lblNewLabelAL = new JLabel("New label");
        lblNewLabelAL.setBounds(170, 292, 56, 16);
        contentPane.add(lblNewLabelAL);

        labelArraylistPos = new JLabel("Arraylist Position");
        labelArraylistPos.setBounds(170, 263, 110, 16);
        contentPane.add(labelArraylistPos);

        textField = new JTextField();
        textField.setBounds(340, 139, 116, 22);
        contentPane.add(textField);
        textField.setColumns(10);
        lblNewLabelcuestion.setText(answer1.get(count));
        btnAnswer = new JButton("answer");

        lblNewLabelAL.setText(answer1.get(count));

        btnAnswer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                String line1;
                for (int j = 0; j < answer1.size(); j++) {
                    line1 = answer1.get(j);
                    textArea.append(line1);
                }

                lblNewLabelcuestion.setText(answer1.get(count));
                lblNewLabelAL.setText(answer1.get(count));
                lblNewLabelTF.setText(textField.getText());

                String c = answer1.get(count);
                String Tf = textField.getText();

                if (Tf.equals(c)) {
                    lblNewLabel_1.setText("Good");
                } else {
                    lblNewLabel_1.setText("Wrong");
                }
                count += 1;
            }
        });
        btnAnswer.setBounds(340, 198, 97, 25);
        contentPane.add(btnAnswer);

        lblNewLabel_1 = new JLabel("New label");
        lblNewLabel_1.setBounds(45, 153, 256, 16);
        contentPane.add(lblNewLabel_1);

        textArea = new JTextArea();
        textArea.setBounds(345, 273, 181, 67);
        contentPane.add(textArea);

        lblNewLabel = new JLabel("achive.text  content");
        lblNewLabel.setBounds(340, 244, 116, 16);
        contentPane.add(lblNewLabel);
    }

}
    
asked by Piter Don 01.10.2017 в 16:05
source

0 answers