I'm trying to write a program that checks if a string contains multiple words that must be occurred in a specific order the words are stored in Array of Strings
Here what I have reached so far
boolean Check = false;
Scanner S = new Scanner(System.in);
System.out.println("What is your question?");
String input=S.nextLine();
String[] Words = {"pay","car"};
for (int i = 0; i <= Words.length -1 ; i++) {
if (input.matches(".*\\b"+Words[i]+"\\b.*") && input.matches(".*\\b"+Words[1]+"\\b.*")) {
Check = true;
}
}
if (Check){
System.out.println("30k Dollar");
} else{
System.out.println("Wrong info! ");
}
Basically, what my code does is when the user input for example "how much should i pay for the car?" he will get an answer of "30k Dollar"
because the strings "pay" and "car" are both in my array of strings.
Case 2: if the user input " bla bla car bla bla pay"
he will get the same answer.
How can I prevent the program from giving the same answer for the 2 different questions?
also in my code I used Words[i] and Words[1] but when I got larger list of words this wont work, I tried using nested loop but it didn't work.