alternative to using or operator java? [duplicate] - java

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)) {
}

Related

Why do I have to use SomeRandomString.equals("something") instead of SomeRandomString == "something" in Java? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Why doesn’t == work on String? [duplicate]
(7 answers)
Closed 2 years ago.
I'm relatively new to Java so I'm kind of lost here. Is it wrong when you are trying to compare two strings just use the identical symbol "==" instead of using a method for checking if two strings are identical. Because for what I have tried the first option doesn't work and I'm very curious on the reason behing it
In java when you use String, you work with reference.
String one = "one";
String two = "two";
if(one == two)
// true if references `one` and `two` point to the same object
if(one.equals(two))
// true if strings `one` and `two` are equals (it could be to different objects)

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

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.

Can we compare a string with == to an empty value [duplicate]

This question already has answers here:
Comparing a string with the empty string (Java)
(9 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
Lets say we have a string in java. Can we compare this string to "" using the ==?
For example:
String myString = "";
if(myString == "");
Of course you can (insofar that compilation will pass), although you will probably not get the result you expect since using == will compare references not contents.
My favourite way is to use the Yoda Expression "".equals(myString) since then you don't need to pre-test myString for null.
Else you could use myString.isEmpty().

regex java, unable to get the value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
Consider a String variable pri with value 07:45:32PM , now in order to obtain the PM alone I applied,
pri = pri.replaceAll("[^A-Z]","");
So far things work fine, but trying to compare the value in the variable does not work, ie :
if(pri=="PM")
{
hh+=12;
}
The body of the loop does not get executed. My question is are the two values different, ie Pri=="PM" , Why is it so? And how do I get to check my if loop in a precise way? Thank you
EDIT1
So I tried if(pri.equals("PM")) instead of if(pri=="PM") , but still it did not solve the problem!
EDIT: Use "equalsIgnoreCase()" method for comparing strings.
if(pri.equalsIgnoreCase("PM"))
{
hh+=12;
}
You need to use .equals not == to compare two String Object.
if(pri.equals("PM"))
Use if(pri.equals("PM")) instead.

Comparing strings - Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm new to Java. And I've following problem:
string s = "someword";
if (s == "someword")
// do something
Sometimes doesn't work for me. Don't know why.
Thanks for responds.
In Java == compare reference. Use .equals() for compare value.
Duplication of this: How do I compare strings in Java?

Categories

Resources