How to compare multiple strings inside an if statement?
You cannot compare a variable against multiple values like that in C++. You should be doing:
if (theString == "Seven" || theString == "seven" || theString == "7")
{
theInt = 7;
cout << "You chose: " << theInt << endl;
}
else if (theString == "Six" || theString == "six" || theString == "6")
{
theInt = 6;
cout << "You chose: " << theInt << endl;
}
How can I compare multiple strings in an if statement with Java?
Your code is written in a very roundabout way.
- There's no point checking the second letter of the string every loop - check it beforehand, and store the result
- Don't use
.split("")
when all you want is a character array - Use a for loop rather than iterating with a while loop
Here's how I'd write it:
String str = arr[i];
char[] letters = str.toCharArray();
boolean secondLetterIsVowel = letters[1] == 'a'
|| letters[1] == 'e'
|| letters[1] == 'i'
|| letters[1] == 'o'
|| letters[1] == 'u';
for(char letter : letters) {
if(secondLetterIsVowel) {
inputField.append(" \n" + letter);
}
//...
}
How to compare if multiple strings are not found in a statement from input variable?
Your specific error is because of using ||
(OR) instead of &&
(AND).
You want to ask "if the input isn't "word1" AND it also isn't "word2" AND it also isn't "word3"", and you can write the condition exactly like that. When you use OR, the if statement will always be true since the input can't be all 3 values at once.
Either way, I agree with Jordan that you ought to use an array.includes()
for checking against many valid inputs.
Best way to check multiple strings in an if statement
1. Regex
if (usercountry.matches("FRA|GER|ITA"))
{
costpermile = costpermile*1.42;
}
2. Add countries to a data structure (Set) and check
Set<String> eurocountries= new HashSet<String>();
eurocountries.add("FRA");eurocountries.add("GER");eurocountries.add("ITA");
if (eurocountries.contains(usercountry))
{
costpermile = costpermile*1.42;
}
Note: I think its the regex method you're looking for
Comparing Multiple Strings Using || Logical Operator Is Not Working Properly in C language
You've got your conditions mixed up.
If you want to 'do something', if it's any of these strings, you need to check for 'equal', as in:
if (!strcmp("rock", game) || !strcmp("paper", game) || !strcmp("scissor", game))
{
//Do something
}
else
{
printf("You entered a wrong input\n");
}
Alternatively you can do a cascade:
if (!strcmp("rock", game))
//Do something for rock
else if (!strcmp("paper", game))
//Do something for paper
else if (!strcmp("scissors", game))
//Do something for scissors
else
printf("Wrong input\n");
This has the same effect with the bonus effect of telling you exactly, which the user has input.
If you wanted to check whether it is NONE of the options, you'll need to use &&
.
Comparing Strings in an IF statement
The reason why this was happening was because the text response from my XMLHttpRequest object was actually "GB " - three characters long.
I just cut it down to two and it worked fine:
cc.substring(0,2)
how to compare two strings in javascript if condition
You could check every option.
if (compare === "page1" || compare === "page2") {
Or you could use an array and check with an existential quantifier like Array#some
against, like
if (["page1", "page2"].some(a => a === compare)) {
var compare = "page3";
if (compare === "page1" || compare === "page2") {
document.body.innerHTML = "github url";
} else {
document.body.innerHTML = "non-github url";
}
Related Topics
C++ How to Use Select to See If a Socket Has Closed
How to Prevent Paging for One Program/Process
Hello World Python Extension in C++ Using Boost
Error Lnk2019: Unresolved External Symbol _Main Referenced in Function _Tmaincrtstartup
Std::Fstream Doesn't Create File
Why Is Stack Memory Size So Limited
How to Find and Replace String
Why Does Long Long N = 2000*2000*2000*2000; Overflow
How to Assert If a Std::Mutex Is Locked
How to Switch Between Blas Libraries Without Recompiling Program
Send Mail Using Smtp in C++ on Linux
How to Create a Single Instance Application in C or C++
Make Shared_Ptr Not Use Delete
Altering Dll Search Path for Static Linked Dll