Java - Opposite of .contains (does not contain)

Tizer1000 picture Tizer1000 · Mar 31, 2013 · Viewed 149.9k times · Source

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?

Answer

Itsan Alias picture Itsan Alias · Jul 10, 2013

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
}