hi all, been tryin to find wat is wrong with this code. It works for sometime then starts repeating the same value continuossssssly. any help will be appreciated. thanks

/* THIS PROGRAM IS TO RUN A SIMULATION PROGRAM OF ARRIVAL AND SERVICE TIMES OF AN AUTO-REPAIR WORKSHOP
 * servTime = serviceTime, depTime = departureTime, arrTime = arrivalTime.
 * **/

package home;
import java.util.ArrayList;
public class Simulation {
        static int arrTime = 0;
        static ArrayList < Integer > depTimes = new ArrayList< Integer >();
        static int simClock = 0;
        static int depTime = 0;
        static boolean createEvent = true;
    public static void main(String[] args) {

    while(simClock <=500){
        if (createEvent == true){ 
        double intrTime = 20*(-2.303*Math.log(Math.random())); //calculate the interval Time of arrival
        double servTime = 25*(-2.303*Math.log(Math.random())); //calculate the service time for each car 

        arrTime += Math.round(intrTime); //cummulate the interval times which is the arrival time on the clock 
        depTime = (int) (arrTime+ Math.round(servTime)); //departure time = arrival +service
        depTimes.add(depTime);

    }
        //this is to arrange in ascending order
        if (arrTime <= min(depTimes)){
            simClock = arrTime;
        }else {simClock = min(depTimes);
            createEvent = false;
            for (int i=0; i<depTimes.size(); i++){
                if (simClock == depTimes.get(i)){
                    depTimes.remove(i);
                }
            }
        }

        System.out.printf("%d\n",simClock);

    }
}
    static int min(ArrayList<Integer> arr){
        int minx = 0;
        int val = arr.get(0);
        for (int depTimex : arr ){

            if (val <= depTimex){
                minx = val;
            }else minx = depTimex;

        }return minx;
    }
}

You have a large while loop

while (simClock <= 500) {
...

then a print statement near the end

System.out.printf("%d\n",simClock);
}

Given the calculation and randoms, simClock is staying less than 500, so the while condition remains true and it repeats the print statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.