String as an "action word"

Hi, What can I do to get a string as an "action word"? Let me explain it. I want give the User a decision "You want to go or wait?" Now the User should type "Go" or "wait" and the code continues with an if statement. Is it possible to do that and is it possible set more words to one action e.g. "Go", "go", "go for it" (that every word is activating the same if statement) Thank you

7/12/2019 9:45:38 AM


6 Answers

New Answer


Sure, strings can be, just like other data types, compared by the == and != operator. If multiple words are supposed to trigger one action, you can chain them inside the if-condition using the OR operator (||). Usually you use a loop that asks the user to choose an option until the input matches one of the options you provided. An important operation that is especially useful here is transforming the string to lowercase, so you need to perform less comparisons (example in the code). Here is an example for such a menu, let me know if you have any questions on it: https://code.sololearn.com/cfkKvb1bDxNR/?ref=app




You can use switch statement


But we can use char in switch


Muaz Ahmad, strings are not numerical values and therefore do not work in switch statements. You need to use an if - else if - else statement.


Indeed, Muaz Ahmad, because you can convert a character to a numerical value, to be more precise, to its ASCII value. Therefore, as you pointed out, we can use characters in switch statements, but not strings, because those can not be converted to a fixed identical numerical value. And since the question was regarding strings, a switch statement would not work for this particular problem.