Hello I am currently trying to create a computer game\program and the main thing that it uses is input from the user, commands, in a JTextField. So I already have a basic text field set up called "question" (JTextField question = new JTextField(15);) in a JFrame and an OK button to enter the text. Next is a String "answer". This is: String answer = question.getText().toUpperCase();
This is how the field is set up. Net is the 3 if statements that give an error if no text is entered, if it is not the right word, and of course, the correct answer. They are as follows:
Public class ButtonListener extends ActionListener
{
publix void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonOK)
{
String answer = question.getText().toUpperCase();
if(answer.length() == 0)
{
JOptionPane.showMessageDialog(
Window.this,
"ERROR: NO INPUT",
"ERROR",
JOptionPane.INFORMATION_MESSAGE);
}
if(answer == "XENIX")
{
JOptionPane.showMessageDialog(
Window.this,
"Welcome, Sir",
"Greetings",
JOptionPane.INFORMATION_MESSAGE);
}
if(answer != "XENIX" && answer.length > 0)
{
JOptionPane.showMessageDialog(
Window.this,
"ERROR: INCORECT PASSWORD",
"ERROR",
JOptionPane.INFORMATION_MESSAGE);
}
This is how the if statements react when text is entered and the ok button is pressed. Everything works but when I type in "Xenix", it says "ERROR: INCORRECT PASSWORD". The, "Welcome, Sir" message never shows, even though using a print method it shows it is clearly getting "XENIX" from the answer. Why is it not working? Is there another method I'm supposed to use? Please respond soon! Thanks!
if(question.getText() !=null && !question.getText().isEmpty())
{
String answer = question.getText().toUpperCase();
if(answer.length() == 0)
{
------------------
------------------
}
if(answer.equals("XENIX"))
{
------------------
------------------
}
if((!answer.equals("XENIX")) && answer.length > 0)
{
------------------
------------------
}
}