0

I'm trying to create a boolean condition for an if statement where it's true if the variable exactly matches any of the following strings, but I keep getting this error:

error: bad operand types for binary operator '||'

My code at the moment is:

if(pProvince != ("Alberta"||"British Columbia"||"Manitoba"||"New Brunswick"||"Newfoundland And Labrador"||"Nova Scotia"||"Ontario"||"Prince Edward Island"||"Quebec"||"Saskatchewan"||"Northern Territories"||"Nunavut"||"Yukon")){
    //do stuff
}else{
    //do other stuff
}

Any suggestions? I'm fairly new to java programming so don't be afraid to lecture me on how to use boolean operators properly.

  • Does this answer your question? [String.equals() with multiple conditions (and one action on result)](https://stackoverflow.com/questions/10208052/string-equals-with-multiple-conditions-and-one-action-on-result) – OH GOD SPIDERS May 23 '23 at 16:31

6 Answers6

4

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

Instead of this, use collection. Declare Set and then add all these values to it. Use set.contains for comparison


Set<String> provinces=new HashSet<>();

provinces.add("Alberta");
provinces.add("British Columbia");

if(provinces.contains(pProvince)) {
//Matched
}

This solution will give you result in O(1)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Chetan Ahirrao
  • 1,454
  • 11
  • 16
1

In java you can not use '||' directly inside parentheses to compare. you can do it only this way.

if (!(pProvince.equals("Alberta") || pProvince.equals("British Columbia") || pProvince.equals("Manitoba") || pProvince.equals("New Brunswick") || pProvince.equals("Newfoundland And Labrador") || pProvince.equals("Nova Scotia") || pProvince.equals("Ontario") || pProvince.equals("Prince Edward Island") || pProvince.equals("Quebec") || pProvince.equals("Saskatchewan") || pProvince.equals("Northern Territories") || pProvince.equals("Nunavut") || pProvince.equals("Yukon"))) {
    // do stuff
} else {
    // do other stuff
}

which is not readable. I suggest you create a List of strings and then check if value contains like:

List<String> provinces = Arrays.asList(
    "Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland And Labrador",
    "Nova Scotia", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan",
    "Northern Territories", "Nunavut", "Yukon"
);

if (!provinces.contains(pProvince)) {
    // do stuff
} else {
    // do other stuff
}
Feel free
  • 758
  • 5
  • 15
0

You could try: pProvince.equals("Alberta") then you get a true or false as result, but its only for one. You can use a string array for all the names and you can check all the names step by step with a "for next loop"

Genaut
  • 1,810
  • 2
  • 29
  • 60
Mand
  • 1
  • 1
  • Thank you for posting an answer! It would be better if you could write what you're suggesting as code so it is clearer what you're trying to say (you can refer to the other answers as examples) – Miss Skooter May 23 '23 at 19:06
0

In Java, to compare String values, use the String.equals method.

boolean value = pProvince.equals("Alberta");

Since you're looking to match a set of values, there are a few approaches for this.

You can create a method which will return a boolean if the argument matches a set of values within an array.

boolean matches(String string) {
    String[] strings = {
        "Alberta", "British Columbia", "Manitoba", "New Brunswick",
        "Newfoundland And Labrador", "Nova Scotia", "Ontario",
        "Prince Edward Island", "Quebec", "Saskatchewan",
        "Northern Territories", "Nunavut", "Yukon"
    };
    for (String value : strings) 
        if (value.equals(string)) return true;
    return false;
}

Another approach, if you're familiar with regular expressions, is the String.matches method, which takes a regular expression pattern as the parameter.

boolean matches(String string) {
    String regex = 
        "Alberta|British Columbia|Manitoba|New Brunswick|" +
        "Newfoundland And Labrador|Nova Scotia|Ontario|" +
        "Prince Edward Island|Quebec|Saskatchewan|" +
        "Northern Territories|Nunavut|Yukon";
    return string.matches(regex);
}
Reilas
  • 3,297
  • 2
  • 4
  • 17
-2

The error message you're seeing suggests that the operands used with the '||' (logical OR) operator in your if statement are not valid. In Java, the '||' operator expects Boolean values (true or false) as operands, rather than strings. To perform string matching in an if statement, you can use the '==' operator with brackets '( ... )' and the '=' operator for pattern matching. Here's an example of how you can check if a variable exactly matches any of the specified strings:

Code:

variable="example"

if ( variable == "string1" || variable == "string2" || variable == "string3" ){ 
  //do stuff
}else{
  //do stuff}

Replace "string1", "string2", and "string3" with the strings you want to match against your variable.

  • You do not compare `String` values in Java with the `==` operator. – Reilas May 23 '23 at 18:45
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 24 '23 at 04:38