-2

I need help with one if condition in java code

    public class Main
    {
        public static void main(String[] args) {
            String sHeaderStatus = "1";
            Boolean hasButton = false;
            Boolean editableLineStatus =true;
            String sFrom = "REQ";
            int canChangeSupplier = 0;
            if ((sHeaderStatus.equals("1") || canChangeSupplier == 1 && 
            (sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
            || hasButton && editableLineStatus && !sHeaderStatus.equals("85")) || sFrom.equals("APPROVAL")) {
                String valdiaton ="true11";
            System.out.println(valdiaton);              
            }       
        }

result is true11

    public class Main
    {
        public static void main(String[] args) {
            String sHeaderStatus = "1";
            Boolean hasButton = false;
            Boolean editableLineStatus =false; //changed this one to false
            String sFrom = "REQ";
            int canChangeSupplier = 0;
            if ((sHeaderStatus.equals("1") || canChangeSupplier == 1 && 
            (sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
            || hasButton && editableLineStatus && !sHeaderStatus.equals("85")) || sFrom.equals("APPROVAL")) {
                String valdiaton ="true11";
            System.out.println(valdiaton);
                
            }
            
        }

result is still true11

I am not able to understood the issue.

Per my understanding...

sHeaderStatus.equals("1") || canChangeSupplier == 1 // gave true
(sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
            || hasButton   // gave false

become true && false && true && true

Similarly second code would become true && false && false &&true

Am not sure how this become true and below line printed.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 4
    Did you try debugging this code to verify what is actually happening? [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Nov 10 '21 at 18:39
  • 2
    what makes you believe that the languages java and javascript are the same? – Mister Jojo Nov 10 '21 at 18:43
  • You're literally printing `true11` each time, so are you trying to ask why anything gets printed at all? – OneCricketeer Nov 10 '21 at 18:51
  • The first code becomes `true || true`, while the second becomes `true || false`; if you want to make sure Java understands the way you want things grouped, add parentheses to make it explicit. – Scott Hunter Nov 10 '21 at 18:54
  • FWIW, you could simplify all the equals checks with something like `sHeaderStatus.matches("10|14|8[5-7]")` – OneCricketeer Nov 10 '21 at 18:58
  • 1
    IMHO: you should *always* use additional parentheses with expressions involving a mix of `||` and `&&` operators. – MC Emperor Nov 10 '21 at 19:07
  • @oneCricketeer. Yes i am not trying to understand how if condition met to the print inside code. – Ankush Jassal Nov 11 '21 at 19:16
  • I am tryi g to understand how these. Conditions works.. per me it start from left to right.. – Ankush Jassal Nov 11 '21 at 19:17
  • @MC Emperor yes i would use it..but this snippet is from one work project. – Ankush Jassal Nov 11 '21 at 19:19
  • As Scott said above, Both are OR-d with true, so both will print the output. Maybe you should explain how you came up with the `true && false && true && true`? – OneCricketeer Nov 11 '21 at 19:22

1 Answers1

0

&& happens before ||

Write the code like this

if (
  (
    sHeaderStatus.equals("1")
    || canChangeSupplier == 1 && (sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
    || hasButton && editableLineStatus && !sHeaderStatus.equals("85")
  ) 
  || sFrom.equals("APPROVAL")
)

You can see that you always will have (something) || false with the values given.

And changing only editableLineStatus will modify only the operator "grouping" for hasButton && editableLineStatus && !sHeaderStatus.equals("85").

However, regardless of what you change that to, you have sHeaderStatus.equals("1"), which is true, resulting in logic of

(true || (false && false) || (false && true/false && true)) || false

which, in total, is true, therefore entering the conditional

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245