This question already has an answer here:
- How do I compare strings in Java? 23 answers
I am working on a code that takes what you input into a window's text box, and answers you accordingly. Currently I am trying to say that if the user enters the word "yes", two windows will display, one saying "k, lets talk", and one saying "you typed in yes". Right now only the second window is opening, and the first two if statements aren't even executing. How can I make this work?
class hear implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if (event.getActionCommand() == "yes" || event.getActionCommand() =="Yes" || event.getActionCommand() =="YES")
JOptionPane.showMessageDialog(null, "k, lets talk");
if (event.getActionCommand() == "no" || event.getActionCommand() =="NO" || event.getActionCommand() == "NO")
JOptionPane.showMessageDialog(null, "k bye!");
if(event.getSource()==textfield)
string = String.format("You typed in %s", event.getActionCommand());
JOptionPane.showMessageDialog(null, string); //Open a new window that displays String string, which has changed based on which field you hit enter on.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire