vendredi 16 mars 2018

Need help scanning user input and printing a string if a key word matches

Hey so im basically trying to make a very simple chatter bot for my school work, iv been trying to figure out how to display a certain text of string when a key word is entered by the user, For example they program will ask "Please choose from on of these subjects (maths,english,science)", and the user will type something like "i would like to choose science" and then the program will scan the users input looking for the keywords 'english, maths and science' and once if finds one of them it will display a string message, if it doesnt find any it will display a string message saying error.

So far iv made one if statment but it only works if the user input exactly as i made the if statment, while i need it to scan the user input and if a key word is user anywhere it will display the sentence.

import java.util.Scanner;
import javax.swing.JOptionPane;

public class ChatterBot {

    public static void main(String args[]) {
        String input = "";
        input = JOptionPane.showInputDialog("What subject do u wanna choose mate (maths,english,science) ");

        if (input.equals("maths")) {
            input = JOptionPane.showInputDialog("Maths is fun and used alot in subjects");
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire