Search code examples
javastringloopswhile-loopor-operator

do While loop trouble


I am having trouble with my do-while statement. I created a do-while loop to ensure the only accepted input is "e" or "o" (not case sensitive), however, it continues the loop even though I inserted the desired input. Any help is appreciated!

Current Code & Result


Solution

  • This statement:
    while(!side.equalsIgnoreCase("O") || !side.equalsIgnoreCase("E"));
    is always true

    If you input E or e, this !side.equalsIgnoreCase("E") is false but this !side.equalsIgnoreCase("O") is true

    If you input O or o, this !side.equalsIgnoreCase("O") is false but this !side.equalsIgnoreCase("E") is true

    Since you are using ||, true || false gives you true so the loop never ends

    For every other input, both are true (true || true) which is also true

    You need to replace it with:
    while(!side.equalsIgnoreCase("O") && !side.equalsIgnoreCase("E"));