-1
import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class batsmen {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    batsmen window = new batsmen();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public batsmen() {
        initialize();
    }

    /**
     * Initialise the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblClue = new JLabel("Clue 1:");
        lblClue.setBounds(20, 6, 61, 16);
        frame.getContentPane().add(lblClue);

        JLabel lblNewLabel = new JLabel("He is the leading international run scorer of all time");
        lblNewLabel.setBounds(48, 30, 375, 16);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblClue_1 = new JLabel("Clue 2");
        lblClue_1.setBounds(20, 69, 61, 16);
        frame.getContentPane().add(lblClue_1);

        JLabel lblHePlayedFor = new JLabel("He played for India");
        lblHePlayedFor.setBounds(48, 94, 375, 16);
        frame.getContentPane().add(lblHePlayedFor);

        JLabel label = new JLabel("");
        label.setBounds(20, 229, 403, 16);
        frame.getContentPane().add(label);
        frame.setVisible(true);

        textField = new JTextField();


        textField.setBounds(20, 191, 290, 26);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblTakeAGuess = new JLabel("Take a Guess:");
        lblTakeAGuess.setBounds(20, 163, 119, 16);
        frame.getContentPane().add(lblTakeAGuess);

        JButton btnNewButton = new JButton("Enter");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String answer = textField.getText();
                answer = answer.toLowerCase();
                if(answer == "sachin tendulkar") {
                    label.setText("Correct");
                }else if (answer != "sachin tendulkar"){
                    label.setText("Incorrect. The answer was Sachin Tendulkar");
                }
            }
        });
        btnNewButton.setBounds(322, 191, 117, 29);
        frame.getContentPane().add(btnNewButton);


    }

}

The if statement does not seem to work as even if the correct answer is inputted, it displays "Incorrect. The answer was Sachin Tendulkar". I would appreciate any help greatly. I have checked and the String answer contains the correct answer, yet the program outputs "Incorrect!". This is extremely confusing and annoying. Once again, I would appreciate any help that you are able to give me. Thanks

1 Answers1

0

You could try

if(answer.equals("sachin tendulkar")) {
    label.setText("Correct");
}

Its generally better to use .equals() for string comparison. You can read why this is here

omeanwell
  • 1,847
  • 1
  • 10
  • 16