Home > Blockchain >  The else-statement is the section of my code that always executes even though the user input is equa
The else-statement is the section of my code that always executes even though the user input is equa

Time:07-16

I am making this simple java GUI project named Dis or Dat where a user inputs two choices first and the answer, then the other user will try to guess the answer given by the first user on the other frame.

btnEnter2.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent f) { 
                            if(optionAnswer.equalsIgnoreCase(optionDis)){
                              if(optionAnswer.equalsIgnoreCase(userAnswer001)){
                                  JOptionPane.showMessageDialog(frame, "You are Correct");
                              } else{
                                  JOptionPane.showMessageDialog(frame, "You are Incorrect");
                              }
                            }
                        }
                    });

And the problem is that when checking the answer of the second user, it always runs the "You are incorrect" JOptionPane in the else statement even if the user's answer is correct.

This is the full code of the GUI

package javathisorthat;

import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Scanner;
import javax.swing.Action;
import javax.swing.JOptionPane;
//import javafx.scene.text.Font;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 *
 * @author Alexander Jon
 */
public class DisOrDatGUI{
    
    private static JLabel labelDis;
    private static JLabel labelOr;
    private static JLabel labelDat;
    private static JLabel labelAnswer;
    private static JTextField disOption;
    private static JTextField datOption;
    private static JTextField answerOption;
    private static JButton btnEnter;
    public static JButton btnEnter2;
    
    public static void guiDOrD(){
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            
            frame.setSize(400,300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.add(panel);
            
            panel.setLayout(null);
            //setBounds(x,y,width,height);
            //text fields for options given by the first user
            //and password field for the answer that the second user will guess
            
            labelDis = new JLabel("Dis");
            labelDis.setBounds(10,20,80,25);
            panel.add(labelDis);
            
            disOption = new JTextField(20);
            disOption.setBounds(100,20,165,25);
            panel.add(disOption);
            
            labelOr = new JLabel("or");
            labelOr.setBounds(10,50,80,25);
            panel.add(labelOr);
            
            labelDat = new JLabel("Dat");
            labelDat.setBounds(10,80,80,25);
            panel.add(labelDat);
            
            datOption = new JTextField(20);
            datOption.setBounds(100,80,165,25);
            panel.add(datOption);
            
            labelAnswer = new JLabel("Answer");
            labelAnswer.setBounds(10,100,80,25);
            panel.add(labelAnswer);
            
            answerOption = new JTextField(20);
            answerOption.setBounds(100,100,165,25);
            panel.add(answerOption);
            
            btnEnter = new JButton("Enter");
            btnEnter.setBounds(100,160,80,25);
            panel.add(btnEnter);
            btnEnter.addActionListener(new ActionListener() { 
                private JLabel userAnswer;
                private JTextField userAnswer01;
                @Override
                public void actionPerformed(ActionEvent e) {
                    JFrame frame2 = new JFrame();
                    JPanel panel2 = new JPanel();
                    frame2.setSize(400,300);
                    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame2.add(panel2);
                    
                    panel2.setLayout(null);
                    
                    //getting the user input from the first frame as reference
                    String optionDis = disOption.getText();
                    String optionDat = datOption.getText();
                    String optionAnswer = answerOption.getText();
                    
                    userAnswer = new JLabel("Type your answer :");
                    userAnswer.setBounds(10,20,165,25);
                    panel2.add(userAnswer);
                    
                    userAnswer01 = new JTextField();
                    userAnswer01.setBounds(200,20,165,25);
                    panel2.add(userAnswer01);
                    
                    String userAnswer001 = userAnswer01.getText();

                    //System.out.print(optionAnswer);
                    
                    JButton btnEnter2 = new JButton("Enter");
                    btnEnter2.setBounds(100,60,80,25);
                    panel2.add(btnEnter2);
                    //After clicking enter it should check if the user input is the same with the answer entered by another user on the first jframe.
                    btnEnter2.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent f) { 
                            if(optionAnswer.equalsIgnoreCase(optionDis)){
                              if(optionAnswer.equalsIgnoreCase(userAnswer001)){
                                  JOptionPane.showMessageDialog(frame, "You are Correct");
                              } else{
                                  JOptionPane.showMessageDialog(frame, "You are Incorrect");
                              }
                            }
                        }
                    });
                    frame2.setVisible(true);
                }
            });
            frame.setVisible(true);      
    }
}

CodePudding user response:

As MadProgrammer said in comment:

"String userAnswer001 = userAnswer01.getText(); is assigning a blank String".

That's true and then you are checking if:

optionAnswer (eg. "some answer") equals to userAnswer001 (that is empty String: "").

You can test it by running your app and giving empty answer. Your program should print "You are Correct".

/

Please also try to name variables move intuitively, eg. firstUser, secondUser, firstUserAnswer (or correctAnswer), secondUserAnswer, questionFrame, answerFrame, etc, because naming variables like: userAnswer01, userAnswer001 can be confusing to other programmers :)

CodePudding user response:

First off, I would highly recommend cleaning up your code a little bit so that you do not get lost in your own code. However, there were two issues in this section of code.

btnEnter2.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent f) {
        if(optionAnswer.equalsIgnoreCase(optionDis)){
            if(optionAnswer.equalsIgnoreCase(userAnswer001)){
                JOptionPane.showMessageDialog(frame, "You are Correct");
            } else{
                JOptionPane.showMessageDialog(frame, "You are Incorrect");
            }
        }
    }
});

Your first issue is that you were using an unneeded 'if' statement that would only mean that the code below it would only be checked if the correct answer was contained in the 'Dis' box.

if(optionAnswer.equalsIgnoreCase(optionDis)){
  //Anything contained in this box would only run if the optionAnswer was equal to the option in the optionDis box.
}

Your second issue is that you never got the contents of the written answer. Due to the fact that you assigned the string variable 'userAnswer001' to be the text of the answer box before the user actually pressed the button to check if they were correct or not.

if(optionAnswer.equalsIgnoreCase(userAnswer001)){ //This is basically the same as running 'if(optionAnswer.equalsIgnoreCase(""))'
  JOptionPane.showMessageDialog(frame, "You are Correct");
}

This code below should fix both of those issues you are having.

btnEnter2.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent f) {
        if(optionAnswer.equalsIgnoreCase(userAnswer01.getText())){ 
            JOptionPane.showMessageDialog(frame, "You are Correct");
        } else {
            JOptionPane.showMessageDialog(frame, "You are Incorrect");
        }
    }
});
  • Related