don't be afraid to lecture me on how to use boolean operators properly
<cracks knuckles>
The most basic thing to know is that boolean operators take boolean operands (an operand is an argument you pass to an operator).
So, in !A
, the operand A
has to be something whose type is boolean
(or Boolean
); and in A || B
, both operands A
and B
have to be things whose type is boolean
(or Boolean
).
In "Alberta" || "BC"
, you're trying to give the ||
boolean operator string operands. This is what it's complaining about.
You need to create a boolean expressions based on the string, e.g.
pProvince.equals("Alberta") // this is either true or false.
or
pProvince.equals("BC") // again, either true or false.
and then combine those that with a boolean operator, e.g.
pProvince.equals("BC") || pProvince.equals("Alberta") // still either true or false
and then, if you want, use other binary operators such as !
, e.g.
!(pProvince.equals("BC") || pProvince.equals("Alberta")) // even now! either true or false
However, there are other, better ways of checking a string against multiple strings.
In this case, I would create a set of strings:
Set<String> provinces = Set.of("Alberta", "BC");
and then check using provinces.contains(pProvince)
. Actually, you could use any kind of Collection
instead of a Set
, e.g. a List
. However, Set
is the most appropriate if all you're doing is invoking contains
, because that can be done more efficiently for implementations of Set
such as a HashSet
(O(1) time) or TreeSet
(O(log N) time).
Another way could be to use a switch
statement:
switch (pProvince) {
case "Alberta":
case "BC":
// ... other provinces
//do other stuff
break;
default:
//do stuff
}
Another option could be to use regular expressions.