Reputation: 129
I would like the while loop to execute whenever the user enters a value other than "stop" or "continue" when ignoring case. How can I do this? Whenever I run the program it prints "Invalid entry" even if I type in continue or stop. What am I doing wrong?
package butt;
import java.util.*; // used for console input
public class butt_face{
static Scanner console = new Scanner(System.in); // needed for user input
public static void main(String[] args) {
String strContinue; // input from console and used for boolean
System.out.println("Would you like to stop or continue? ");
strContinue = console.next();
while(!(strContinue.equalsIgnoreCase("stop")) || !(strContinue.equalsIgnoreCase("continue")))
{
System.out.println("Invalid entry");
System.out.println("Would you like to stop or continue? ");
strContinue = console.next();
} // end while
if(strContinue.equalsIgnoreCase("stop")
{
System.out.println("Good bye!");
} // end if
else
System.out.println("Poop Ship Destroyer");
} // end of main
} // end of class
Upvotes: 1
Views: 102
Reputation: 8021
You need to replace with a && in the condition instead of || Check the repl https://repl.it/B6GJ/0
Upvotes: 1
Reputation: 150
The issue is in your logic.
while(!(strContinue.equalsIgnoreCase("stop")) || ! (strContinue.equalsIgnoreCase("continue")))
This part checks if strContinue is equal to stop if it is equal to stop it will then check to see strContinue is equal to continue.
Upvotes: 0
Reputation: 1738
You have the wrong operator:
!(strContinue.equalsIgnoreCase("stop"))
is true
unless strContinue
is "stop"
.
!(strContinue.equalsIgnoreCase("continue"))
is true
unless strContinue
is "continue"
.
strContinue
cannot be both "stop"
and "continue"
, therefore at least one of the conditions will always be true
, and false || true
is true
.
Solution: change the or (||
) to an and (&&
).
Upvotes: 7
Reputation: 36
Do something like a input function that calls the input scanner until you entered one of these two strings.
Upvotes: -5