nmu
nmu

Reputation: 1519

How to check for multiple possible values of a string in Java

I want to perform the same operation for more than one value of a string called contents

if (contents == "chicken roll" , contents == toasted chicken){
      // do some common operation
    }

What is the proper Java syntax for this?

Upvotes: 0

Views: 342

Answers (1)

Mark Byers
Mark Byers

Reputation: 838306

I think you want a logical or ||:

if (contents.equals("chicken roll") || contents.equals("toasted chicken")){

See the list of operators.

Also note that you compare for string equality using the equals method.

Upvotes: 2

Related Questions