0

I cannot find my error in my program, which is supposed to add and subtract ints to a queue and then print out the average and max of each queue over 500 minutes. I keep getting an out of bounds exception error but cannot solve the problem. Any help determining where my issue is would be greatly appreciated!

import java.util.*;
import java.util.Random;
import java.util.LinkedList;
import java.util.ArrayList;

public class AirportQueue {
    public static int longestWaitTime = 0;
    public static Random rand = new Random(3);
    public static int partySize;
    public static Random randQueue = new Random(2);
    public static Queue<Integer> airportQ1 = new LinkedList<>();
    public static Queue<Integer> airportQ2 = new LinkedList<>();
    public static Queue<Integer> airportQ3 = new LinkedList<>();
    public static ArrayList[] minute = new ArrayList[500];
    public static ArrayList[] secondMinute = new ArrayList[250];
    public static ArrayList<Integer> q1 = new ArrayList(0);
    public static ArrayList<Integer> q2 = new ArrayList(0);
    public static ArrayList<Integer> q3 = new ArrayList(0);
    public static int avgQ1 = 0;
    public static int avgQ2 = 0;
    public static int avgQ3 = 0;

    public void airportSimulation() {

        for (int i = 0; i < 500; i++) {
            if ((i % 2) == 0) { // add a party to a queue each minute
                if ((airportQ1.size() < airportQ2.size()) && (airportQ1.size() < airportQ3.size())) {
                    partySize = rand.nextInt();
                    airportQ1.add(partySize);
                    q1.add(partySize);
                } else if ((airportQ2.size() < airportQ1.size()) && (airportQ2.size() < airportQ3.size())) {
                    partySize = rand.nextInt();
                    airportQ2.add(partySize);
                    q2.add(partySize);
                } else {
                    partySize = rand.nextInt();
                    airportQ3.add(partySize);
                    q3.add(partySize);
                }
                int queueSelected = randQueue.nextInt();
                if (queueSelected == 0) {
                    if (airportQ1.isEmpty()) {
                        System.out.println("The first queue is empty.");
                    } else {
                        airportQ1.remove();
                    }
                } else if (queueSelected == 1) { // remove a party every 2 minutes
                    if (airportQ2.isEmpty()) {
                        System.out.println("The second queue is empty.");
                    } else {
                        airportQ2.remove();
                    }
                } else {
                    if (airportQ3.isEmpty()) {
                        System.out.println("The third queue is empty.");
                    } else {
                        airportQ3.remove();
                    }
                }

            } else {
                if ((airportQ1.size() < airportQ2.size()) && (airportQ1.size() < airportQ3.size())) {
                    partySize = rand.nextInt();
                    airportQ1.add(partySize);
                    q1.add(partySize);
                } else if ((airportQ2.size() < airportQ1.size()) && (airportQ2.size() < airportQ3.size())) {
                    partySize = rand.nextInt();
                    airportQ2.add(partySize);
                    q2.add(partySize);
                } else {
                    partySize = rand.nextInt();
                    airportQ3.add(partySize);
                    q3.add(partySize);
                }
            }
        }

        // num of people in queue
        for (int k = 0; k < airportQ1.size(); k++) {
            int q1Total = 0;
            avgQ1 = (q1Total + q1.get(k)) / 500;
        }
        for (int k = 0; k < airportQ2.size(); k++) {
            int q1Total = 0;
            avgQ2 = (q1Total + q2.get(k)) / 500;
        }
        for (int k = 0; k < airportQ3.size(); k++) {
            int q1Total = 0;
            avgQ3 = (q1Total + q3.get(k)) / 500;
        }

        // Max value for each queue
        int max1 = q1.get(0);
        for (int i = 0; i < q1.size(); i++) {
            int current1 = q1.get(i);
            if (current1 > max1) {
                max1 = current1;
            }
        }

        int max2 = q2.get(0);
        for (int i = 0; i < q2.size(); i++) {
            int current2 = q2.get(i);
            if (current2 > max2) {
                max2 = current2;
            }
        }

        int max3 = q3.get(0);
        for (int i = 0; i < q3.size(); i++) {
            int current3 = q3.get(i);
            if (current3 > max3) {
                max3 = current3;
            }
        }

        System.out.println("The average number of people in the first "
                + "Airport Security queue is " + avgQ1);
        System.out.println("The average number of people in the second "
                + "Airport Security queue is " + avgQ2);
        System.out.println("The average number of people in the third "
                + "Airport Security queue is " + avgQ3);
        System.out.println("The maximum number of people in the first "
                + "queue at any given time was " + max1);
        System.out.println("The maximum number of people in the first "
                + "queue at any given time was " + max2);
        System.out.println("The maximum number of people in the first "
                + "queue at any given time was " + max3);

    }

    public static void main(String[] args) {
        AirportQueue testAirport = new AirportQueue();
        testAirport.airportSimulation();
    }
}

Stacktrace:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
     at java.util.ArrayList.rangeCheck(Unknown Source)
     at java.util.ArrayList.get(Unknown Source)
     at AirportQueue.airportSimulation(AirportQueue.java:93)
     at AirportQueue.main(AirportQueue.java:134)

Line in question:

int max1 = q1.get(0);
6
  • And how did you run this Commented Feb 16, 2014 at 4:11
  • It would help if you told us from what line the out-of-bounds exception is being thrown. Commented Feb 16, 2014 at 4:14
  • This logic is clearly flawed - int q1Total = 0; avgQ1 = (q1Total + q1.get(k))/500; hint (every avgQ1 = the last element/500). Commented Feb 16, 2014 at 4:18
  • 1
    I added the stacktrace, the problem is this statement: int max1 = q1.get(0); which means the size of q1 is 0. Commented Feb 16, 2014 at 4:18
  • 1
    @lchristina26 following on what Mr. Polywhirl wrote, that means that on that pass through the loop, it never hit a q1.add() call, which is understandable given that there are 4 other paths it could have taken through the first series of if blocks. (Indeed if i is odd, it won't even go through the if-blocks at all. So what happens when i is 1 and nothing has been added to q1 on the previous i==0 pass through the loop? Commented Feb 16, 2014 at 4:22

1 Answer 1

1

Solved the problem, thanks for the help to all the comments! Below is the corrected code that works well enough for the class that it was assigned in.

import java.util.Random;
import java.util.LinkedList;
import java.util.ArrayList;

public class AirportQueue {

public Random rand = new Random();
public int partySize;
public Random randQueue = new Random();
public LinkedList<Integer> airportQ1 = new LinkedList<>();
public LinkedList<Integer> airportQ2 = new LinkedList<>();
public LinkedList<Integer> airportQ3 = new LinkedList<>();
public ArrayList[] minute = new ArrayList[500];
public ArrayList[] secondMinute = new ArrayList[250];
public ArrayList<Integer> q1 = new ArrayList(0);
public ArrayList<Integer> q2 = new ArrayList(0);
public ArrayList<Integer> q3 = new ArrayList(0);
public int avgQ1 = 0;
public int avgQ2 = 0;
public int avgQ3 = 0;
public int max1 = 0;
public int max2 = 0;
public int max3 = 0;
public int countQ1 = 0;
public int countQ2 = 0;
public int countQ3 = 0;

public void airportSimulation() {

    int queueSelected = randQueue.nextInt(3);

    //Simulate queues for 500 minutes
    for (int i = 1; i < 501; i++) {
        //add a party to a queue each minute
        if ((i % 2) == 0) {
            if ((airportQ1.size() <= airportQ2.size()) && (airportQ1.size() <= airportQ3.size())) {
                partySize = rand.nextInt(4) + 1;
                airportQ1.add(partySize);
                q1.add(partySize);
                System.out.println("Minute " + i + ": a party of " + partySize + " just entered the queue.");
            } else if ((airportQ2.size() <= airportQ1.size()) && (airportQ2.size() <= airportQ3.size())) {
                partySize = rand.nextInt(4) + 1;
                airportQ2.add(partySize);
                q2.add(partySize);
                System.out.println("Minute " + i + ": a party of " + partySize + " just entered the queue.");
            } else//((airportQ3.size() <= airportQ1.size()) && (airportQ3.size() <= airportQ2.size())) 
            {
                partySize = rand.nextInt(4) + 1;
                airportQ3.add(partySize);
                q3.add(partySize);
                System.out.println("Minute " + i + ": a party of " + partySize + " just entered the queue.");
            }

            //remove a party every 2 minutes
            if (queueSelected == 0) {
                if (airportQ1.size() != 0) {
                    airportQ1.remove();
                    if (airportQ1.size() > 0) {
                        System.out.println("Minute " + i + ": a party of " + airportQ1.get(airportQ1.size() - 1) + " just passed security.");
                    }
                }
            } else if (queueSelected == 1) {
                if (airportQ2.size() != 0) {
                    airportQ2.remove();
                    if (airportQ2.size() > 0) {
                        System.out.println("Minute " + i + ": a party of " + airportQ2.get(airportQ2.size() - 1) + " just passed security.");
                    }
                }
            } else {
                if (airportQ3.size() != 0) {
                    airportQ3.remove();
                    if (airportQ3.size() > 0) {
                        System.out.println("Minute " + i + ": a party of " + airportQ3.get(airportQ3.size() - 1) + " just passed security.");
                    }
                }
            }
            queueSelected = randQueue.nextInt(3);

        } else {
            if ((airportQ1.size() <= airportQ2.size()) && (airportQ1.size() <= airportQ3.size())) {
                partySize = rand.nextInt(4) + 1;
                airportQ1.add(partySize);
                q1.add(partySize);
                System.out.println("Minute " + i + ": a party of " + partySize + " just entered the queue.");
            } else if ((airportQ2.size() <= airportQ1.size()) && (airportQ2.size() <= airportQ3.size())) {
                partySize = rand.nextInt(4) + 1;
                airportQ2.add(partySize);
                q2.add(partySize);
                System.out.println("Minute " + i + ": a party of " + partySize + " just entered the queue.");
            } else {
                partySize = rand.nextInt(4) + 1;
                airportQ3.add(partySize);
                q3.add(partySize);
                System.out.println("Minute " + i + ": a party of " + partySize + " just entered the queue.");
            }
        }

        //num of people in queue
        int q1Total = 0;
        for (int k = (airportQ1.size() - 1); k > 0; k--) {
            q1Total = q1Total + airportQ1.get(k);
            avgQ1 = q1Total / 500;
            q1.add(q1Total);
        }
        int q2Total = 0;
        for (int k = 1; k < airportQ2.size(); k++) {
            q2Total = q2Total + q1.get(k);
            q2.add(q2Total);
        }
        int q3Total = 0;
        for (int k = 1; k < airportQ3.size(); k++) {
            q3Total = q3Total + airportQ3.get(k);
            q3.add(q3Total);
        }
    }

    //avg number of people in each queue
    for (int m = 1; m < q1.size(); m++) {
        countQ1 = q1.get(m) + countQ1;
    }
    for (int m = 1; m < q2.size(); m++) {
        countQ2 = q2.get(m) + countQ2;
    }
    for (int m = 1; m < q3.size(); m++) {
        countQ3 = q3.get(m) + countQ3;
    }
    avgQ1 = countQ1 / (q1.size() - 1);
    avgQ2 = countQ2 / (q2.size() - 1);
    avgQ3 = countQ3 / (q3.size() - 1);

    //Max value for each queue
    for (int j = 0; j < q1.size(); j++) {
        int current1 = q1.get(j);
        if (current1 > max1) {
            max1 = current1;
        }
    }
    for (int i = 0; i < q2.size(); i++) {
        int current2 = q2.get(i);
        if (current2 > max2) {
            max2 = current2;
        }
    }
    for (int i = 0; i < q3.size(); i++) {
        int current3 = q3.get(i);
        if (current3 > max3) {
            max3 = current3;
        }
    }

    System.out.println("The average number of people in the first "
            + "Airport Security queue is " + avgQ1);
    System.out.println("The average number of people in the second "
            + "Airport Security queue is " + avgQ2);
    System.out.println("The average number of people in the third "
            + "Airport Security queue is " + avgQ3);
    System.out.println("The maximum number of people in the first "
            + "queue at any given time was " + max1);
    System.out.println("The maximum number of people in the second "
            + "queue at any given time was " + max2);
    System.out.println("The maximum number of people in the third "
            + "queue at any given time was " + max3);

}

public static void main(String[] args) {
    AirportQueue testAirport = new AirportQueue();
    testAirport.airportSimulation();
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.