0

I have the following enum

public enum State {
    UNCHECKED, SUBMITTED, VALIDATED, REJECTED, ENTITLED;
}

and in another class I have a method that receives a State as an argument, like this

public void validate (State state) throws InvalidStateException {
    if (state != State.VALIDATED || state != State.REJECTED) 
        throw new InvalidStateException(); 
}

this method should throw the exception InvalidStateException only when the argument State isn't either State.VALIDATED or State.REJECTED but is in fact throwing it always. I just can't see where the mistake can be or how could I write the code in another way that would work.

I know that this isn't working because the following JUnit Test says the mentioned exception is being thrown and it shouldn't:

@Test
public void testValidate () throws InvalidStateException {
    object.validate(State.VALIDATED);
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423

1 Answers1

2

The condition state != State.VALIDATED || state != State.REJECTED is always true. the only way for it to be false would be for state to be VALIDATED and REJECTED at the same time.

You want &&, not ||.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255