-4

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());
                }
            }
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Dc5nipe
  • 119
  • 1
  • 6
  • 2
    Use this: `if ("MR".equals(l.getPlot()) || "X".equals(l.getPlot())) ...` you need to check equality each time. Also, put the string literal first, to avoid a null pointer exception – Tim Biegeleisen Jul 18 '18 at 15:29

1 Answers1

4

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.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    Improved? Beg to differ. If null checks are needed, I prefer to make them explicit or use `Objects.equals`. [Yoda conditions](https://en.wikipedia.org/wiki/Yoda_conditions) – Ole V.V. Jul 18 '18 at 15:50