john
john

Reputation: 201

my program doesn't check the else-if statement

i'm still new to coding so please bear with me. my program always check what's going on in the if statement and doesn't go to else-if statement even the value is false. So, why does that happen? Any help would be greatly appreciated.

for (int i = 0; i <= time; i++) { System.out.println("Clock loop = " + i);

        for (int j = 0; j < cashierArr.length; j++) {
            System.out.println("CashierArr at " + j + "  is busy? "
                    + cashierArr[j].busy);
            System.out.println(j);

            if (cashierArr[j].busy = true) {

                cashierArr[j].serveTime -= 1;
                System.out.println("Serve time = "
                        + cashierArr[j].serveTime);
                System.out.println("In busy");

            } else if (cashierArr[j].busy = false) {


                Customer tempCust = (Customer) queue.poll();


                cashierArr[j].serveTime = tempCust.serviceTime;
                System.out
                        .println("serveTime = " + cashierArr[j].serveTime);
                cashierArr[j].busy = true;

                System.out.println("In not busy");
            }

        }

Upvotes: 2

Views: 108

Answers (4)

Pravin Fofariya
Pravin Fofariya

Reputation: 346

1) if(cashierArr[j].busy = true)   

2) else if (cashierArr[j].busy = false)

In above statement you are use (=)assignment operator not (==)comparison operators.

= Operator is use for assign value not comparison so It's actually assign the

value to true means it is always result true so your else statement never gets to be execute ever.

change == in both statement

1) if(cashierArr[j].busy == true)   

2) else if (cashierArr[j].busy == false)

Upvotes: 1

when you do this:

if (cashierArr[j].busy = true) {

you are assigning the variable to true and not checking the value in there...

and since you are checking booleans it it enough to write:

if (cashierArr[j].busy) {

Upvotes: 0

meet thakkar
meet thakkar

Reputation: 84

It is a basic syntax issue change

it assigns value true and false to cashierArr[j].busy

if (cashierArr[j].busy = true) to if (cashierArr[j].busy == true)

and

else if (cashierArr[j].busy = false) to else if (cashierArr[j].busy == false)

Assuming your code might be using Boolean for busy field and you dont want to do anything in case of null.

Upvotes: 0

Vishal Gajera
Vishal Gajera

Reputation: 4207

please , do following ,

if (cashierArr[j].busy ==  true) { // == is comparison operator, while = is assign

you have used only single assignment which will actually assign the value to true means it's always remains true

if (cashierArr[j].busy =  true)

Upvotes: 5

Related Questions