Question:
So, I would like to check for multiple words in an if statement (words that mean “yes”, “No” and so on.
I don’t know how to check the multiple conditions without using many x == z || y == z ||…
Any ideas are welcome!
Does it need to be in java? I know how to do it in python
yes_responses = ['yes', 'yeah', 'sure', 'ok', 'okay', 'thats right']
no_responses = ['no', 'nope', 'nah', 'no banana you can't do it', 'never']
user_response = input("Do you want to proceed using Java? ").lower()
if user_response in yes_responses:
print("Continuing...")
elif user_response in no_responses:
print("Stopping...")
else:
print("Give me a proper answer")
What would be the java version of
if user_response in yes_responses
I believe that if you are using arrays, this will work.
import java.util.Arrays;
// in some condition
Arrays.asList(arr).contains(toCheckValue);
how would you use this in this example
out.println("Do you want cheese? ($" + cheesePrice + "0)");
input.nextLine();
while (cheese == "") {
cheese = input.nextLine();
Start.clear();
if (cheese.toUpperCase().equals("YES")) {
out.println("OK");
total += 0.50;
} else if (cheese.toUpperCase().equals("NO")) {
out.println("OK");
} else {
out.println(REDTEXT + "Absolutely NOT!" + DEFAULTTEXT);
cheese = "";
}
It would be,
if (yes_responses.contains(user_response)) {}
Assuming that yes_responses
is of type String[]
Should be:
'no banana you can\'t do it'
2 Likes
That’s true, naive mistake of mine hahahaha
1 Like
Thank you. I will try to implement this asap.
One thing, this only works for an arraylist.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.