mercredi 9 août 2017

How to make if/else statements read first matching keywords? (Java)

How do you implement a condition for the first matching keyword in a ChatBot program? The code below is current code i'm using:

public static void firstMatchingKeyword1() {
    String sex;
    sex = JOptionPane.showInputDialog(null, "Are you a guy or woman?");
            if (sex.contains("guy")) {
        JOptionPane.showMessageDialog(null, "I am a guy too");  
            } else if (sex.contains("woman")) {
            JOptionPane.showMessageDialog(null, "I am a woman too");
    }
}

The problem is that if both keywords are presents, I want the output to be the first keyword typed. For example : If user typed "I am a woman but also a man". I want the output to be only the message "I am a woman too" instead of going through all the if statements one by one.

Aucun commentaire:

Enregistrer un commentaire