简体   繁体   中英

While loop doesn't continue even though condition still holds true

I was tasked to create a simulation of a Bank. So far the progress I've made are generating random Client objects, making 4 objects of Teller, putting the Client objects into a circular queue and putting a Client into an empty Teller if there are any.

The problem I have now is that the while loop I made does no continue even though the set condition is still true, it can only execute 5 times. The program still runs though.

This is the code I have so far(assume all variables are declared):

public void run() {
    int counter = 1;
    populateTellers(windowArray);
    while (counter < 10) {
        newClient = generateClient();

        System.out.println(newClient.toString() + " arrived at :" + counter);
        System.out.println();

        clientQueue.enqueue(newClient);
        clientList.add(newClient);
        System.out.println("The Queue now contains " + clientQueue.numberOfOccupiedCells() + " Client(s):");
        for (int x = 0; x < clientList.size(); x++) {
            System.out.println(clientList.get(x).toString());
        }
        System.out.println();

        arrayIndex = getATeller(windowArray);
        if (arrayIndex != -1) {
            clientList.remove(0);
            clientQueue.dequeue();
            windowArray[arrayIndex].setClient(newClient);
            windowArray[arrayIndex].setServiceTime(newClient.getDuration());
            System.out.println(windowArray[arrayIndex].getName() + " now serves " + newClient.toString());
        } else {
            for (int x = 0; x < clientList.size(); x++) {
                if (windowArrray[arrayIndex].getServiceTime() == counter) {
                    windowArray[arrayIndex].setClient(new Client());
                    System.out.println(windowArray[arrayIndex].getName() + " ends service for " + newClient.toString());
                }
            }
        }
        counter++;
    }
}

The one thing that I can't get head around is that when I remove the code from aI = getATeller(wA); 'till the closing bracket of the if statement below it the program would work. I've tried putting the block into a method, changing the positions of the codes inside if(){} and else{} and changing the condition to if(aI==1) , still I got no improvements. Any idea on what I'm doing wrong?

EDIT: So I've been able to narrow it down to a line of code which is windowArray[arrayIndex].setServiceTime(newClient.getDuration()); , whenever I assign a value to a Teller's service time the problem would occur, I've checked my Teller class and there was no error there. Any ideas how I can solve this.

Why is it only executing 5 times? According to the code, counter is used as the used to check if it should continue looping for not. The only instance where counter is changed is at last line: counter++ .

I can guess that you did a System.out.print on the counter, and it ends up to 5, and it magically stops. Most likely it's throwing an exception, which is not caught, and is lost in the void.

Edit: If this piece of code is being run on another thread, then maybe it's hanging up on some function. Do as told here

Try to find where your loop was turning to infinite loop,

Try writing printing statements, wherever necessary, just to find the flow of execution.

Then printout your variable values, too check whether they were as expected, keep on narrowing your scope as you find error occurred in certain scope, then you will find the culprit, if it is leading you to some other function, then thoroughly check that particular function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM