Search code examples
javacontains

Opposite of .contains (does not contain)


I have an if statement:

if (inventory.contains("bread"))

But now I want to check

  • if inventory contains "bread"
  • but does not contain "water".

How can I do this?


Solution

  • It seems that Luiggi Mendoza and joey rohan both already answered this, but I think it can be clarified a little.

    You can write it as a single if statement:

    if (inventory.contains("bread") && !inventory.contains("water")) {
        // do something
    }