For some reason, when I have multiple correct strings, the statement keeps repeating
do {
System.out.println("Enter Service Code");
Scanner a = new Scanner(System.in);
serviceCode = a.nextLine();
} while (!serviceCode.equals("ORB1") || !serviceCode.equals("ORBH") ||
!serviceCode.equals("ISS5") || !serviceCode.equals("ILLOYDS") ||
!serviceCode.equals("DLAB") || !serviceCode.equals("LEOM7") ||
!serviceCode.equals("MOON2"));
However, when there's just one string that the code checks against. The do while statement works fine and will stop looping when the correct input is entered
do {
System.out.println("Enter Service Code");
Scanner a = new Scanner(System.in);
serviceCode = a.nextLine();
} while (!serviceCode.equals("ORB1"));
If you enter "ORB1", "!serviceCode.equals("ORB1")" will return false but the others will return true; and you are using the "OR" operator. So, this sentence :
!serviceCode.equals("ORB1") || !serviceCode.equals("ORBH") ||
!serviceCode.equals("ISS5") || !serviceCode.equals("ILLOYDS") ||
!serviceCode.equals("DLAB") || !serviceCode.equals("LEOM7") ||
!serviceCode.equals("MOON2")
will always be true. You need to use the "AND" operator
!serviceCode.equals("ORB1") && !serviceCode.equals("ORBH") &&
!serviceCode.equals("ISS5") && !serviceCode.equals("ILLOYDS") &&
!serviceCode.equals("DLAB") && !serviceCode.equals("LEOM7") &&
!serviceCode.equals("MOON2")
Your comparison can never return false. it's either A or B.
so, if you were to say:
if ( !A OR !B ){
--> Input = A => true (because !B returns true)
--> Input = B => true (because !A returns true)
--> Input = C => true (because !A returns true)
Change your OR (||) by AND (&&)
Also: declare and instantiate your Scanner before your loop.
A better approach would be create a Listof string which includes the valid codes and check if that list contains the provided user input.
List<String> validServiceCodes = Arrays.asList("ORB1", "ORBH", "ISS5", "ILLOYDS", "DLAB", "LEOM7", "MOON2" );
do {
System.out.println("Enter Service Code");
Scanner a = new Scanner(System.in);
serviceCode = a.nextLine();
} while (!validCodes.contains(validServiceCodes));
Related
I want to make my do loop run while the input the user made is not equal to the required letters (a-i) For some reason,even when i input the proper letters, it loops forever.
I've tried using switch cases as well as != inside the comparison.
Here is my code:
do {
System.out.println("Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
lL1=in.nextLine();
if (!lL1.equals("a")||!lL1.equals("b")||!lL1.equals("c")||!lL1.equals("d")||!lL1.equals("e")||!lL1.equals("f")||!lL1.equals("g")||!lL1.equals("h")||!lL1.equals("i")){
System.out.println("Invalid Input. Try again.");
}//End if statement
}while(!lL1.equals("a") || !lL1.equals("b") || !lL1.equals("c") || !lL1.equals("d") || !lL1.equals("e") || !lL1.equals("f") || !lL1.equals("g") || !lL1.equals("h") || !lL1.equals("i"));
My skills in Java are limited but this should work, unless i'm missing something obvious. Any help would be amazing!
Instead of using an operator for each case of the input, you might want to create a list of the accepted answers and then your condition will look like:
while answer is not in accepted answers, ask another input
An example would be:
Scanner scanner = new Scanner(System.in);
List<String> acceptedAnswers = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i");
String input;
do {
System.out.println(
"Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
input = scanner.nextLine();
} while (!acceptedAnswers.contains(input));
scanner.close();
System.out.println("Got correct input: " + input);
If you have a negation you need AND to join the conditions not OR.
That's because if you or some not-ed values, they form an and.
Let me explain better.
If you input a, then the first is false (because you not it), and the others are true, so the or condition make the result be true.
You should instead group all the ors and then not it.
e.g.
!(lL1.equals("a") || lL1.equals("b") || lL1.equals("c") || lL1.equals("d") || lL1.equals("e") || lL1.equals("f") || lL1.equals("g") || lL1.equals("h") || lL1.equals("i"))
Please try this:
char lL1;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
lL1=scanner.next().charAt(0);
}while(lL1!='a' && lL1!='b' && lL1!='c' && lL1!='d' && lL1!='e' && lL1!='f' && lL1!='g' && lL1!='h' && lL1!='i');
Since you are only getting a single character, you can check that it does not match either [a to i] characters as shown above. This is the shortest way to do so by making the check as the condition of the loop. If it fails then the loop will be called.
I was trying to make an user input in an do while loop. The loop should continue if the user input is not yes OR not.
When I write:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String userInput = "";
String yes = "yes";
String no = "no";
do {
System.out.print("Type yes or no: ");
userInput = input.readLine();
} while (userInput.equals(yes) || userInput.equals(no));
If the user type "yes" OR "no" the loop will continue, all other inputs will result the end of the loop.
When I write:
while (!userInput.equals(yes) || !userInput.equals(no));
The loop continues everytime, no matter what the user enters.
Only when I use the AND operator it works.
while (!userInput.equals(yes) && !userInput.equals(no));
Why is that so?
Should it not be the OR operator because I want to say, if the user enters yes OR no stop the loop.
!userInput.equals(yes) || !userInput.equals(no)
Let's say the user enters "hello". You ths have
!false || !false
which is
true || true
which is
true
Let's say the user enters "yes".
You thus have
!true || !false
which is
false || true
which is
true
Let's say the user enters "no".
You thus have
!false || !true
which is
true || false
which is
true
So you see that, whetever the user enters, the condition is always true, and thus the loop continues forever.
Think about the Boolean expressions you are writing. When the users enters 'yes', it evaluates as !true || !false which is equivalent to false||true which is true. Since the condition is satisfied, the loop continues.
Because this is how boolean expressions work. The opposite of A || B is not !A || !B but !(A || B) which is the same as !A && !B.
if( A || B) means either A true or B true. If say A is true then it won't check B it will directly go inside the block. So when you say if(!yes || !no) and if you enter yes then second condition becomes true and when you enter no the first condition becomes true and it continues to execute. And if you enter abdc its again true.
In my program, I have a String called yesOrNo that is a keyboard input. I created an if statement to test if yesOrNo is one of the following : "Y", "y", "Yes",
"yes" by using the || operator.
I got the error message: The operator || is undefined for the argument type(s) java.lang.String, java.lang.String. What is the right way to do something like this? Thanks.
Scanner keyboard = new Scanner(System.in);
String yesOrNo = keyboard.nextLine();
System.out.println(yesOrNo + "?" );
if (yesOrNo.equals("Y" || "y" || "Yes || "yes")){
The shortest I can think of is :
if (yesOrNo.equalsIgnoreCase("Y") || yesOrNo.equalsIgnoreCase("Yes"))
Your syntax is invalid. It needs to have separate clauses:
if(yesOrNo.equals("Y") || yesOrNo.equals("y")...)
or cleaner would be if you used regex:
if(yesOrNo.matches("Y|y|Yes|yes")) {
// Code.
}
Extra Reading
You should look at the String Docs. They detail all sorts of useful stuff.
Read up on Regex. It makes complex String comparison very simple.
Finally, look at the different Operators to see what kind of logical statements you can form, with the correct syntax.
Alternatively, you could create a list of acceptable answers and check whether the answer is in that list.
List<String> kindOfYes = Arrays.asList("yes", "y", "okay", "righto");
if (kindOfYes.contains(yesOrNo.toLowerCase())) { ...
Two ways:
Using equals:
if (yesOrNo.equals("Y") ||
yesOrNo.equals("y") ||
yesOrNo.equals("Yes") ||
yesOrNo.equals("yes")) {
//...
}
Using regexp (shorther than using || multiple times):
if (yesOrNo.toLowerCase().matches("y|yes")) {
//...
}
Try:
if(yesOrNo.equals("Y") || yesOrNo.equals("y")
|| yesOrNo.equals("Yes") || yesOrNo.equals("yes"))
if (yesOrNo.equals("Y") || yesOrNo.equals("y") || yesOrNo.equals("Yes") || yesOrNo.equals("yes"))
What about the next code?
String yesOrNo = keyboard.nextLine();
if (yesOrNo.toLowerCase().charAt(0) == 'y') {
//
}
NOTE: Do you think there's a quicker way? I think not.
Like this:
if (yesOrNo.equals("Y") || yesOrNo.equals("y") || yesOrNo.equals("Yes") || yesOrNo.equals("yes")){
//...
}
Your program syntax is wrong.
This is correct:
Scanner keyboard = new Scanner(System.in);
String yesOrNo = keyboard.nextLine();
System.out.println(yesOrNo + "?" );
if(yesOrNo.equals("Y") || yesOrNo.equals("y") || yesOrNo.equals("Yes") || yesOrNo.equals("yes")) {
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having trouble with this method, I'm forever stuck on "Sorry you must specify a Deeppan or thin base, try again: " even if I type jimmy, harry, deeppan, thin, Thin.. etc After I type either deeppan or thin, I want the String stored in the variable "size" and returned
Any ideas what I'm missing?
public String Input(){
String size;
Scanner sc = new Scanner(System.in);
System.out.println("thin or thick: ");
do {
size = scan.next();
if ( !size.equalsIgnoreCase( "thick") || !size.equalsIgnoreCase( "thin" )) {
System.out.print("Sorry you must specify a thick or thin base, try again: ");
} else {
break;
}
} while ( true );
return size;
}
Change your || for a && in the if condition. At the moment the condition will be true if you type in 'thin' because it is not deeppan and vice versa.
The statement inside your if is always true.
You should use either
!size.equalsIgnoreCase( "Deeppan") && !size.equalsIgnoreCase( "thin" )
or
!(size.equalsIgnoreCase( "Deeppan") || size.equalsIgnoreCase( "thin" ))
while(true) will make your loop infinite as its always true
reason is you are doing || operation
reason:
!size.equalsIgnoreCase( "Deeppan") || !size.equalsIgnoreCase( "thin" )
size cannot be "Deeppan" and "thin" at the same time :-)
try:
do {
size = scan.next();
if (size.equalsIgnoreCase( "Deeppan") || size.equalsIgnoreCase( "thin" )) {
break;
}
else {
System.out.print("Sorry you must specify a Deeppan or thin base, try again: ");
}
} while ( true );
ps. "size" isn't the best variable name...
No reason to use a do-while, try a while like this, the condition checking becomes more straightforward:
while (true) {
size = scan.next();
if (size.equalsIgnoreCase("deeppan") || size.equalsIgnoreCase("thin"))
return size;
System.out.println("Sorry you must specify a Deeppan or Thin base, try again:");
}
It will always be true, and therefore never reach the else
Example 1: Size is "Deeppan":
!size.equalsIgnoreCase( "Deeppan") || !size.equalsIgnoreCase( "thin")
= !true || !false
= false || true
= true
Example 2: Size is "foo":
!size.equalsIgnoreCase( "Deeppan") || !size.equalsIgnoreCase( "thin")
= !false || !false
= true || true
= true
Yuor statement
if ( !size.equalsIgnoreCase( "Deeppan") || !size.equalsIgnoreCase( "thin" ))
is always true.
import javax.swing.JOptionPane;
public class Insurance {
final static String INPUT_GENDER = "Please enter your gender: (Male or Female)";
final static String MALE = "male";
final static String FEMALE = "female";
static String gender;
public static void main(String args[])
{
do
{
gender = JOptionPane.showInputDialog(INPUT_GENDER).toLowerCase();
System.out.println(gender);
}
while(!gender.equals(MALE) && !gender.equals(FEMALE));
}
}
The above piece of code is the beginning to a revision assignment, but I came across something I don't understand. The user is asked to enter their gender, as "Male" or "Female", and the program should only continue if the input satisfies one of these inputs. This is done by comparing the input to the final strings for MALE and FEMALE.
What I don't understand is why it only works using && in the while statement. I expected it to need ||, because we want it to continue if the input matches either of the two gender strings. I understood that && should only allow the code to continue if both arguments are true.
TL;DR
while(!gender.equals(MALE) && !gender.equals(FEMALE)); //This works
while(!gender.equals(MALE) || !gender.equals(FEMALE)); //This does not work
while(gender.equals(MALE) || gender.equals(FEMALE)); //This does not work
&& is a logical and operator
|| is the logical or operator
Using De Morgan, the following:
while(!gender.equals(MALE) && !gender.equals(FEMALE))
Can be translated to:
while(!(gender.equals(MALE) || gender.equals(FEMALE)))
(note the additional parenthesis and the placement of the ! before them).
Both the above mean that the gender is neither MALE or FEMALE.
Your other code:
while(!gender.equals(MALE) || !gender.equals(FEMALE))
The above means - gender is not MALE or gender is not FEMALE.
while(gender.equals(MALE) || gender.equals(FEMALE));
Similarly, the above means - gender is MALE or gender is FEMALE.
In English: "when not FEMALE or MALE, do again"
then in Java do{...}while(!(FEMALE || MALE));
and !(A||B) is equal to !A && !B
In your do-while loop you want prompt the user to re-enter his/her gender if the gender entered is neither MALE nor FEMALE.
So the while condition would be that if both gender.equals(MALE) and gender.equals(FEMALE) return false then you would re-prompt the user i.e. the loop will iterate.
That means if not of gender.equals(MALE) and not of gender.equals(FEMALE) both are true then your loop should iterate.
Hence,
while(!(gender.equals(MALE)) && !(gender.equals(FEMALE)))
&& is Java's logical AND operator
|| is Java's logical OR operator
for example:
boolean a = true;
boolean b = false;
boolean c = a && b; // c is false
boolean d = a || c; // d is true
Additionally, ! is Java's logical NOT operator. It is used for negating boolean expressions:
boolean p = true;
boolean q = !p; // q is false
It is also worth noting that x && y will result in the same logical value as !(!x || !y).
Similarly, x || y can be rewritten as !(!x && !y)