How to code logical expression 'equals "MR" or "X" '? [duplicate] - java

This question already has answers here:
Compare one String with multiple values in one expression
(20 answers)
String.equals() with multiple conditions (and one action on result) [duplicate]
(7 answers)
Closed 4 years ago.
Need help with if else statement in Java. Need the program to output when either l.getPlot().equals("MR") or ("X") and if l.getZone().equals("UP SPEC") set the top upper limit.
Can anyone explain to me how to properly set that up so when the query is a match for MR or X it will set the top upper.
Note: If I remove || ("X") it works for all the MR items but leaves all the ("X") blank.
if (l.getPlot().equals("MR")) || ("X"){
if (l.getZone().equals("UP SPEC")) {
limit.setTopUpper(l.getLimit());
} else if (l.getZone().equals("LO SPEC")) {
limit.setTopLower(l.getLimit());
}
}

This should do the trick because it applies the logical "or" to two conditions, instead of one condition and one bare string.
if (l.getPlot().equals("MR")) || (l.getPlot().equals("X")){
Here is an improved version (credits Tim Biegeleisen), which avoids a null pointer exception.
if ("MR".equals(l.getPlot()) || "X".equals(l.getPlot()))
If the argument to equals() evaluates to NULL, the result is a clean false.

Related

alternative to using or operator java? [duplicate]

This question already has answers here:
String.equals() with multiple conditions (and one action on result) [duplicate]
(7 answers)
Compare one String with multiple values in one expression
(20 answers)
Is there a simpler way to check multiple values against one value in an if-statement? [duplicate]
(12 answers)
Closed 2 years ago.
I understand the the or operator is only for boolean expressions. So I was wondering if there is a good way to go about doing the code below without using the or operator?
if(hh.getName() != "apple" || "Health Potion" || "roatisary chicken" || "backpack")
One possible solution if these are all related in some way is to store them in a Set of String objects. It is worth noting that this solution depends on each String being case sensitive.
Set<String> values = new HashSet<>(Arrays.asList("apple", "Health Poition", "roatisary chicken", "backpack"));
Then, check if set contains the String hh.getName()
if (values.contains(hh.getName()) {
}
A better solution
A potentially even better solution would be to use an enumeration instead of a bunch of Strings.
enum Item {
APPLE,
HEALTH_POTION,
ROATISARY_CHICKEN,
BACKPACK
}
Set<Item> items = ImmutableMap.copyOf(EnumSet.allOf(Item.class));
Item myItem = ...;
if (items.contains(myItem)) {
}

How does java if statement handle null values? [duplicate]

This question already has answers here:
Why is order of expressions in if statement important
(9 answers)
short-circuiting behavior of conditional OR operator(||)
(3 answers)
Closed 3 years ago.
I have stumbled upon this odd behaviour while working with jasper soft reports and I would like to have it explained since I don't know much about Java.
Basically, I have a print when expression:
($P{Parameter_name}!=0 || $P{Parameter_name}==null)
? true : false
-> this returns false
($P{Parameter_name}==null || $P{Parameter_name}!=0)
? true : false
-> this returns true
The only difference is the equation order but logically it shouldn't make a difference.
The parameter is null.
I know that for example SQL wouldn't care for the order and would always evaluate true with a simple or statement like this.
If the parameter is null, you will get a NullPointerException when evalulating
$P{Parameter_name}!=0
And maybe this exception will prevent Jasper from checking the second condition.
In your second version, the test
$P{Parameter_name} == null
will be evaluated to true, which means that
$P{Parameter_name} != 0
won't be evaluated at all (and no NPE will be raised)

Why is the output "false10" and not "false11"? [duplicate]

This question already has answers here:
Why isn't the right side of && operator evaluated? [duplicate]
(4 answers)
Closed 3 years ago.
Could someone tell me why the value of a is not changing in the following code?
int a = 10;
System.out.print( (a < 5) && (++a==11));
System.out.println(a);
You're seeing "short circuit evaluation" in action. The second part of the boolean expression (++a==11) is never evaluated, because (a < 5) is false. In this case, the JVM knows that the whole expression is false before it evaluates (++a==11), so it skips it entirely.
This is also a great example of why such "side effects" are bad in logical tests: you're mutating the values you're evaluating in an unpredictable way. In a non-trivial program, you don't necessarily know whether (a < 5) is true, so it's difficult to know whether a will be incremented or not.

if( fn1(args) || fn2(args){ "Add the elements into the list" ;} [duplicate]

This question already has answers here:
&& (AND) and || (OR) in IF statements
(9 answers)
Closed 6 years ago.
I have a doubt of how this if loop is going to work. if fn1 evaluates to be true, will it still go for checking fn2 or will it go into the if loop and add the elements into the list?
The || operator is short-circuited, meaning that if the left-hand operand is true (whatever that means in the language in question), the right-hand operator isn't evaluated at all. So in your example, fn1 will definitely be called, but fn2 will only be called if fn1 returns false.

Java substring issue [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
EDIT: Thank you, PakkuDon
instead of using "==" I must use ".Equals()"!
I'm trying to implement a chat-command system though I've encountered troubles in differentiating between standard chat and command chat..
Focusing on just one command for now, `highlight
the output whenever I type in `highlight is:
highlight
`highlight
Here's my code:
String cmd = InMessage.message.substring(0, 10);
System.out.println(cmd);
System.out.println("`highlight");
if( cmd == "`highlight" )
{
... cancel chat packet and proces command
}
and yet the if statement returns false.
What's going on here? Anything I've done is wrong?
You have a number of problems. Firstly, that substring is going to throw an exception if the user types something that's less than 10 characters. Secondly, you're using == where equals would work better. But you don't need either of these things. You could just use
if (InMessage.message.startsWith("`highlight")) {
// whatever
}

Categories

Resources