I have an array in which I am trying to ask the user a question, have them give their answer (and in their answer will be a string value in the array), and then use their answer to formulate a response to them. I tried using .equals and changing the array into a list. Right now I am using a boolean to find if the string from the array is present in the user's answer or not, but I want the code to be very flexible (which means I want it not to matter if the user says "Hi" or "hi". Here's the code below:
String[] albums = {"positions", "thank u, next", "sweetener", "k bye for now",
"dangerous woman", "my everything", "yours truly"};
String firstQuestion = ari.nextLine();
boolean isFound0 = firstQuestion.contains(albums[0]);
if (isFound0) {
System.out.println("tysm! i love " (albums[0]) " as well!");
}
CodePudding user response:
you can go over the albums array with an foreach-loop and if "firstQuestion " is matched you set "isFound0" to true and break out of the loop with break; to improve the string matching you can either use a library or do some quick and dirty operations on both strings before comparing them to reduce false negatives.
firstQuestion.toLowerCase().replaceAll("[^a-z]", ""); // makes all letters lowercase and removes all characters that aren't letters