I'm getting a operator undefined error when I use || - java

I'm trying to get the code to check if an input either has nothing and is entered or doesn't have either Y or N, but I keep getting an error.
I've tried using just " | " but that doesn't work, and " , " just doesn't make sense.
while (answerToFirstQuestionYN.isEmpty() && !answerToFirstQuestionYN.equalsIgnoreCase("Y" || "N")) {
System.out.println("Input not recognized, try again.");
answerToFirstQuestionYN = reader.next();
}
The error message is " The operator "||" is undefined for the argument type(s) java.lang.String, java.lang.String "

The || operator can only join boolean expressions. So you need to separate the conditions to:
!(answerToFirstQuestionYN.equalsIgnoreCase("Y") ||
answerToFirstQuestionYN.equalsIgnoreCase("N"))
Your first condition (answerToFirstQuestionYN.isEmpty()) needs to be removed, otherwise the whole condition is only true if the string is empty.

Java is not natural language. In English, we can say "if answer is Y or N", where "or" can join noun phrases. In Java, "or" only applies to truth values; and expressions are evaluated as in math, with whatever is in parentheses first. answer.equals("Y" || "N") would try to evaluate "Y" or "N" first, which is, from Java's perspective, nonsense. What you really need to do is to first have two truth values: "if answer is Y, or if answer is N":
answer.equals("Y") || answer.equals("N")

Related

How to put 2 condition in one statement actiolistener in java? [duplicate]

I'm a beginner in coding. I was recently working with to create a chatting programme where a user will chat with my computer. Here is a part of the code:
System.out.println("Hello, what's our name? My name is " + answer4);
String a = scanner1.nextLine();
System.out.println("Ok, Hello, " + a + ", how was your day, good or bad?");
String b = scanner2.nextLine();
**if (b.equals("good"))** { //1
System.out.println("Thank goodness");
} else **if (b.equals("it was good"))** { //2
System.out.println("Thank goodness");
} else **if (b.equals("bad"))** { //3
System.out.println("Why was it bad?");
String c = scanner3.nextLine();
System.out.println("Don't worry, everything will be ok, ok?");
String d= scanner10.nextLine();
} else **if (b.equals("it was bad"))**{ //4
System.out.println("Why was it bad?");
String c = scanner3.nextLine();
System.out.println("Don't worry, everything will be ok, ok?");
String d= scanner10.nextLine();
}
if(age<18){System.out.println("How was school?");}
else if (age>=18){System.out.println("How was work?");}
The conditions of the if statements are in Bold (surrounded with **). In case of first and the second condition I want my application to do same thing. Similarly third and fourth condition. I thought it was possible to somehow group them in if statement.
I tried with below code but it doesn't compile:
if (b.equals("good"), b.equals("it was good")) {
System.out.println("Thank goodness");
} else if (b.equals("bad"),(b.equals("it was bad"))) {
System.out.println("Why was it bad?");
String c = scanner3.nextLine();
System.out.println("Don't worry, everything will be ok, ok?");
String d= scanner10.nextLine();
}
Can someone correct it for me?
You can use logical operators to combine your boolean expressions.
&& is a logical and (both conditions need to be true)
|| is a logical or (at least one condition needs to be true)
^ is a xor (exactly one condition needs to be true)
(== compares objects by identity)
For example:
if (firstCondition && (secondCondition || thirdCondition)) {
...
}
There are also bitwise operators:
& is a bitwise and
| is a bitwise or
^ is a xor
They are mainly used when operating with bits and bytes. However there is another difference, let's take again a look at this expression:
firstCondition && (secondCondition || thirdCondition)
If you use the logical operators and firstCondition evaluates to false then Java will not compute the second or third condition as the result of the whole logical expression is already known to be false. However if you use the bitwise operators then Java will not stop and continue computing everything:
firstCondition & (secondCondition | thirdCondition)
Here are some common symbols used in everyday language and their programming analogues:
"," usually refers to "and" in everyday language. Thus, this would translate to the AND operator, &&, in Java.
"/" usually refers to "or" in everyday language. Thus, this would translate to the OR operator, ||, in Java.
"XOR" is simply "x || y but both cannot be true at the same time". This translates to x ^ y in Java.
In your code, you probably meant to use "or" (you just used the incorrect "incorrect solution" :p), so you should use "||" in the second code block for it to become identical to the first code block.
Hope this helped :)
You're looking for the "OR" operator - which is normally represented by a double pipe: ||
if (b.equals("good") || b.equals("it was good")) {
System.out.println("Thank goodness");
} else if (b.equals("bad") || b.equals("it was bad")) {
System.out.println("Why was it bad?");
String c = scanner3.nextLine();
System.out.println("Don't worry, everything will be ok, ok?");
String d= scanner10.nextLine();
}
This is probably more answer than you need at this point. But, as several others already point out, you need the OR operator "||". There are a couple of points that nobody else has mentioned:
1) If (b.equals("good") || b.equals("it was good")) <-- If "b" is null here, you'll get a null pointer exception (NPE). If you are genuinely looking at hard-coded values, like you are here, then you can reverse the comparison. E.g.
if ("good".equals(b) || "it was good".equals(b))
The advantage of doing it this way is that the logic is precisely the same, but you'll never get an NPE, and the logic will work just how you expect.
2) Java uses "short-circuit" testing. Which in lay-terms means that Java stops testing conditions once it's sure of the result, even if all the conditions have not yet been tested. E.g.:
if((b != null) && (b.equals("good") || b.equals("it was good")))
You will not get an NPE in the code above because of short-circuit nature. If "b" is null, Java can be assured that no matter what the results of the next conditions, the answer will always be false. So it doesn't bother performing those tests.
Again, that's probably more information than you're prepared to deal with at this stage, but at some point in the near future the NPE of your test will bite you. :)
You can have two conditions if you use the double bars(||). They mean "Or". That means only ONE of your conditions has to be true for the loop to execute.
Something like this:
if(condition || otherCondition || anotherCondition) {
//code here
If you want all of conditions to be true use &&. This means that ALL conditions must be true in order for the loop to execute. if any one of them is false the loop will not execute.
Something like this:
if(condition && otherCondition && anotherCondition) {
//code here
You can also group conditions, if you want certain pairs of them to be true. something like:
if(condition || (otherCondition && anotherCondition)) {
//code here
There is a simpler way.
if (b.contains("good")) {
...
}
else if (b.contains("bad")) {
...
}

When would you use the conditional operator in java (? :) instead of the if else statement?

I have a piece of code that uses a conditional operator to compare the value of the variable x to 5:
(x >=5 ? x : -x)
What benefit is there to using a conditional operator over a regular if else statement?
Note that is ?: is an expression - it returns a value which must be consumed
z = (x >=5 ? x : -x)
The if construct in Java is not an expression (there are languages where it is) and does not return a value so in this sense they are not equivalent.
An example where they are equivalent is when the if options perform an assignment:
if("Cheese".equals(x)) {
type = "Dairy";
} else {
type = "Maybe not dairy";
}
this is the same as
type = "Cheese".equals(x) ? "Dairy" : "Maybe not dairy";
You can nest ?: to arbitrary depth but really shouldn't - it becomes quite difficult to read:
List<String> cheeses = Arrays.asList("Gouda", "Edam");
String x= "Gouda";
String type = cheeses.contains(x) ? "Gouda".equals(x) ? "Yummy Gouda" : "Cheese - but not Gouda" : "Maybe not dairy";
Ternary operator is not the equivalent of if-else in EVERY possible case. This is because both of possible results of using ternary operator are return values (e. g. simple printing to the console is not a return value so it can't be one of possible results in ternary operator).
Look, you can do it with if-else, but it's not possible with ternary operator:
if (x > 5) {
System.out.println("Statement is true");
else {
System.out.println("Statement is false");
}
You can't do it with ternary operator:
x > 5 ? System.out.println("Statement is true") : System.out.println("Statement is false");
When both of results of using ternary operator are considered as return values, they are then an equivalent of if-else.
Yes, it can be used, but off course in that case using -x doesn't make sense so it depends on what do you want to return when String is an input. You can do this:
("Cheese".equals(x) ? <some object of expected type X> : <some other object of expected type X>)
where X can be String on any other type. And remember to do "string".equals(x) instead of x.equals("string"). This prevents NullPointerException.
For primitive values you can use ==, but for objects you need to use equals method. There are some edge cases when you can use == on String, but they are not relevant to your case I guess and you can read about it outside of this topic.
PS: Some answers talk about if else statement, but that's not what question is about.
Can this be used like an if else on all accounts? For example, can you
compare strings with it?
I am pretty sure that by saying like an if else he means conditional operator which has if else like behaviour, not if else instruction itself.
Yes it can be used in Java; however note that x =="Cheese" isn't the proper way to compare strings in Java, you want something like "Cheese".equals(x).

Java - using ".contains" in the opposite manner

I want to be able to print a string that doesn't contain the words "Java", "Code" or "String", though I am unsure on how to achieve this as I thought this would be achieved by using '!' (NOT). However, this is not the case as the string is still printed despite the inclusion of the words I want to forbid.
Any advice on how to achieve this would be greatly appreciated, thanks in advance.
System.out.println("Type in an input, plez?");
String userInput6 = inputScanner.nextLine();
if (!userInput6.toLowerCase().contains("Java") || !userInput6.toLowerCase().contains("Code") || !userInput6.toLowerCase().contains("String")) {
System.out.println("I see your does not string contain 'Java', 'Code' or 'String, here is your string:- " + userInput6);
} else {
System.out.println("Your string contains 'Java, 'Code' or 'String'.");
}
I thought this would be achieved by using '!' (NOT)
It is. You just haven't applied it correctly to your situation:
You start with this statement:
userInput6.toLowerCase().contains("java") ||
userInput6.toLowerCase().contains("code") ||
userInput6.toLowerCase().contains("string")
which checks if the input contains any of these, and you wish to negate this statement.
You can either wrap the entire statement in parentheses (()) and negate that:
!(userInput6.toLowerCase().contains("java") ||
userInput6.toLowerCase().contains("code") ||
userInput6.toLowerCase().contains("string"))
or apply the DeMorgan's law for the negation of disjunctions which states that the negation of a || b is !a && !b.
So, as Carcigenicate stated in the comments, you would need
!userInput6.toLowerCase().contains("java") &&
!userInput6.toLowerCase().contains("code") &&
!userInput6.toLowerCase().contains("string")
instead.
Your statement is simply checking if the string doesn't contain at least one of these substrings. This means the check would only fail if the string contained all of these strings. With ||, if any operand is true, the entire statement is true.
Additionally, mkobit makes the point that your strings you are checking for should be entirely lowercase. Otherwise, you are checking if a .toLowerCased string contains an uppercase character - which is always false.
An easier way to think of it may be to invert your if statement:
if (userInput6.toLowerCase().contains("Java") ||
userInput6.toLowerCase().contains("Code") ||
userInput6.toLowerCase().contains("String")) {
System.out.println("Your string contains 'Java, 'Code' or 'String'.");
} else {
System.out.println("I see your does not string contain 'Java', 'Code' or 'String, here is your string:- " + userInput6);
}
Since you're using logical OR, as soon as one your contains checks it true, the entire condition is true. You want all the checks to be true, so you need to use logical AND (&&) instead
As #mk points out, you have another problem. Look at:
userInput6.toLowerCase().contains("Java")
You lower case the string, then check it against a string that contains an uppercase. You just removed all uppercase though, so that check will always fail.
Also, you can use regexp :)
boolean notContains(String in) {
return !Pattern.compile(".*((java)|(code)|(string)).*")
.matcher(in.toLowerCase())
.matches();
}
Or just inline it:
System.out.println("Type in an input, plez?");
String userInput6 = inputScanner.nextLine();
if (!Pattern.compile(".*((java)|(code)|(string)).*")
.matcher(userInput6.toLowerCase())
.matches()) {
System.out.println("I see your does not string contain 'Java', 'Code' or 'String, here is your string:- " + userInput6);
} else {
System.out.println("Your string contains 'Java, 'Code' or 'String'.");
}

Why should I always use || instead of | and && instead of &?

Since I started programming Java, I've noticed that everyone was using && and || instead of & and |. What is the reason for this? I've been using && and || all this time because I didn't know you can use & and | on booleans.
class A{static{
boolean t = true;
boolean f = false;
System.out.println("&ff " + (f&&f) + " " + (f&f));
System.out.println("&ft " + (f&&t) + " " + (f&t));
System.out.println("&tf " + (t&&f) + " " + (t&f));
System.out.println("&tt " + (t&&t) + " " + (t&t));
System.out.println("|ff " + (f||f) + " " + (f|f));
System.out.println("|ft " + (f||t) + " " + (f|t));
System.out.println("|tf " + (t||f) + " " + (t|f));
System.out.println("|tt " + (t||t) + " " + (t|t));
}}
As far as I can tell, they are the same:
$ javac A.java && java A
&ff false false
&ft false false
&tf false false
&tt true true
|ff false false
|ft true true
|tf true true
|tt true true
Exception in thread "main" java.lang.NoSuchMethodError: main
Does using || and && improve my code in any way?
As a simple test, I replaced hundreds of occurrences with the short form, and all of my unit tests still pass.
|| and && uses short circuit evaluation
From same article
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation
denotes the semantics of some Boolean operators in some programming
languages in which the second argument is only executed or evaluated
if the first argument does not suffice to determine the value of the
expression
Consider you have an object and you want to check one of its property for some value if you do:
if(obj != null & obj.ID == 1)
If your object obj is null you will get a null reference exception, since you used single &, the first condition evaluate to false, but it will still continue to the next condition and raising null reference exception.
If you used && then the second condition will never get evaluated, thus no exception.
if(obj != null && obj.ID == 1)
With your code you will not see any difference, but using bitwise | or & with multiple conditions would result in no short circuiting at all.
More Explanation.
consider that you following code:
Boolean conditionA = 2 > 1; //some condition which returns true
if(conditionA | obj.SomeTimeConsumingMethod()){
}
Now in above code snippet, suppose you have an object with a method SomeTimeConsumingMethod which takes a lot of time in processing and returns true or false. If you use single | it would evaluate both the conditions, since first conditionA is true it will process obj.SomeTimeConsumingMethod as well. End result will be true for the whole if statement since | (OR) is used.
If your condition is using double || (OR Logical operator)
if(conditionA || obj.SomeTimeConsumingMethod())
Then the second condition obj.SomeTimeConsumingMethod() will not be evaluated. This is short circuiting and that just saved you from executing some time consuming method. Still the end result is true regardless of what was returned from obj.SomeTimeConsumingMethod().
As has already been pointed out, && and || do short-circuit evaluation. & and |, with boolean operands, do the same operations but evaluate both operands regardless of the value of the first operand.
There are cases where it matters which you use, such as when the left operand is a pre-condition for exception-free evaluation of the right hand operand.
In most cases, it does not matter. I use && and || in those cases for readability, because that is the commoner choice. Using & or | with boolean operands would tend to make most Java programmers have to stop and think, and ask themselves whether there is some specific reason for it. I suspect it may be so common because of the inheritance from C and C++.
|| is logical or, where | is the bitwise operation or. Same with && and &. Use && and || if you're in an if statement, and | or & if you're doing bit operations.
While you may get the "correct" result using the bit-wise operators, understand they do NOT represent the same thing. For instance, this works
public static void main(String[] args){
int x = 5;
int y = 9;
System.out.println(x&y); // prints "1"
}
However, this won't
public static void main(String[] args){
int x = 5;
int y = 9;
System.out.println(x&&y); // Compile error: java: operator && cannot
// be applied to int,int
}
The reason you don't use bit-wise operators for boolean logic is so that it's clear what the code is trying to do (as well as knowing that it actually works). Bit-wise operators are for manipulating bit values, where as boolean operators are for evaluating first-order logic statements.
While not the same, it is along the same reasoning for using .equals() vs == for comparison -- You don't want readers guessing as to what you meant to do. It may work in some instances (like comparing constant, hard-coded strings with ==), but it is poor form because what it says is that you are asking for instance equality as opposed to value equality, which is usually implied. And using bit-wise operators implies you want to do bit manipulation, not first-order logic evaluation.
Arguing that short circuit evaluation is mandatory because of performance is weak. You can make an argument that is almost as weak: that short circuit evaluation encourages extra nondeterminism and side channels. Random optimizations or random loss of nondeterminism/side channels, I'd prefer the latter.
The only remaining argument is this: If your code base relies on short circuit behaviour, e.g., the trick described by Habib (if (obj != null && obj.ID == 1)), then it may not be a good idea to start mixing in &, because programmers might get it confused with &&. i.e., if you use &/| everywhere, but only use && in a few places where you rely on short circuit evaluation, someone might make an error and put only &, it may be hard to spot. In this case, if you relied on && for performance instead of something like null checking, it may be harder to spot when someone accidentally puts & alone, since there would be no stacktrace.
Does using || and && improve my code in any way?
Let me demonstrate two examples on how the conditional boolean operators may improve your code. Compare
if (a != null && a.equals("b") || z != null && z.equals("y"))
System.out.println("correct");
with
boolean b1 = a != null, b2 = z != null;
if (b1) b1 = b1 & a.equals("b");
if (b2) b2 = b2 & z.equals("y");
if (b1 | b2) System.out.println("correct");
which is the best way I can think of relying solely on the logical boolean operators.
Second, say you have two tests to perform: simpleTest() and heavyTest(), where the latter involves making an HTTP request and parsing the response. You have
if (simpleTest() & heavyTest()) System.out.println("success");
else throw new IllegalStateException();
and want to optimize it. What would you rather do, this:
if (simpleTest()) {
if (heavyTest()) System.out.println("success");
else throw new IllegalStateException("operation could not be completed");
} else throw new IllegalStateException("operation could not be completed");
this:
boolean b = simpleTest();
if (b) b = b & heavyTest());
if (b) System.out.println("success");
else throw new IllegalStateException("operation could not be completed");
or this:
if (simpleTest() && heavyTest()) System.out.println("success");
else throw new IllegalStateException();
Why should I always use || instead of | and && instead of &?
You shouldn't always use them, but you would do yourself a favor by making them your default because in 99.9% of uses they either don't hurt or make your code much better. Reserve the logical operators for that 0.1% of cases where you truly do need their semantics.
It has been mentioned elsewhere on this page that relying on the conditional evaluation of the right operand makes for counterintuitive code. I refuse to accept such a view on intuition because, by definition, intuition is a learned skill. It is a facility bestowed to you by evolution to recognize a pattern in situations you encounter often, and to quickly (and subconciously) cut through to the correct conclusions. One of the most important aspects of learning a language is acquiring the right intuitions. The argument that a language feature is bad because it is counterintuitive to someone who looks at it for the first time cannot be taken seriously.
In Java (and C/C++ if I remember correctly) the single "|" and "&" are bit-wise operators, not logical operators, they are used for completely different things.
You use the "&&" and "||" for boolean statements
You use the "&" and "|" for bit operations, given the following are binary numbers
dont make assumption so fast. It because this wont compile:
static boolean f(boolean a) {
boolean x = (true|a) ? a : x;
return x;
}
Bro.java:6: variable x might not have been initialized
boolean x = (true|a) ? a : x;
^
and this will:
static boolean f(boolean a) {
boolean x = (true||a) ? a : x;
return x;
}
&& and || are short cut operators. For example:
A && B
Condition B will not be evaluated if condition A is false
A || B
Condition B will not be evaluated if condition A is true
You should always use shortcut operators for multiple conditions evaluation. | or & are used for bit-wise operations.

What does "?" mean in Java? [duplicate]

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 10 years ago.
I dont know what the question mark (?) stand for in java, I was doing a small program, a Nim-game. were looking in a book, for help and saw this statement:
int pinsToTake = (min >= 2) ? 2 : 1;
I don't understand it, what will ? represent, can it be something to do with if-statement but you put it in a variable? and the : can be something "else"? (this things that I just said can be very misleading)
someval = (min >= 2) ? 2 : 1;
This is called ternary operator, which can be used as if-else. this is equivalent to
if((min >= 2) {
someval =2;
} else {
someval =1
}
Follow this tutorial for more info and usage.
Its ternary operator also referred to as the conditional operator, have a look reference
like Object bar = foo.isSelected() ? getSelected(foo) : getSelected(baz);
eg. operand1 ? operand2 : operand3
if operand1 is true, operand2 is returned, else operand3 is returned
operand1 must be a boolean type
operand1 can be an expression that evaluates to a boolean type
operand1 and operand2 must be promotable numeric types or castable object references, or null
if one of operand2 or operand3 is a byte and the other a short, the type of the returned value will be a short
if one of operand2 or operand3 is a byte, short or char and the other is a constant int value which will fit within the other operands
range, the type of the returned value will be the type of the other
operand
otherwise, normal binary numeric promotion applies
if one of operand2 or operand3 is a null, the type of the return will be the type of the other operand
if both operand2 and operand3 are different types, one of them must be compatible (castable) to the other type
reference
it means:
if(min >= 2)
someval =2;
else
someval =1
Its called a ternary operator
See this java example too
That's a Ternary Operator. Check Oracle's doc for further info. Long story short, it is an if-else statement that can be done in a single line and used inside methods and to define variable values.
Syntax:
boolean_expression ? do_if_true : do_if_false;
Parallelism with if-else statement:
if(boolean_expression)
//do_if_true;
else
//do_if_false;
I didn't use brackets on purpose, since you can only execute one line of code in do_if_true and do_if_false.
Example of use:
boolean hello = true;
String greetings = hello ? "Hello World!" : "No hello for you...";
This will set someString as "Hello World!" since the boolean variable hello evaluates to true. On the other hand, you can nest this expressions:
boolean hello = true;
boolean world = false;
String greetings = hello ? (world ? "Hello World!" : "Hello Stranger!") : "No hello for you...";
In this case, greetings will have as a value "Hello Stranger!";
It's called the Ternary If operator, it's just short-hand for an if...else
"? :" is a ternary operator equivalent to an if else statement.
In your example:
pinsToTake = (min >= 2) ? 2 : 1
if min >= 2 then assign 2 to pinsToTake, else assign 1
max = (a > b) ? a : b;
(a > b) ? a : b; is an expression which returns one of two values, a or b. The condition, (a > b), is tested. If it is true the first value, a, is returned. If it is false, the second value, b, is returned. Whichever value is returned is dependent on the conditional test, a > b. The condition can be any expression which returns a boolean value.
It is called conditional operator.This is how it works.
if min is greater than or equal to 2 ,then first value after ? that is 2 here will be assign to corresponding variable ,otherwise second value that is 1 here will be assign.
This link will tell you all you need.
Summary for archival sakes:
It's called the conditional operator. It's a ternary operator that
takes three terms:
BooleanExpression ? Expr1 : Expr2
The BooleanExpressionis evaluated. If it's true, the value of the
whole expression is Expr1. If it's false, the value of the whole
expression is Expr2.
So it serves the same kind of purpose as an if statement, but it's a
term rather than a whole statement. That means you can embed it in
places where you can't use a whole statement.

Categories