For my Java homework I had to make a program that simulates 100,000 people going 25 times around a monopoly board and finding the percentage times a space is landing on. I get this error when I run it.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 39
    at Monopoly.analyze(Monopoly.java:86)
    at TestMonopoly.main(TestMonopoly.java:6)

Here is my main code.

public class Monopoly
{
    String[] names = new String[39];
    Dice roller = new Dice();
    final int numPlayers = 100000;
    final int timesAround = 25;

    public void setUpBoard()
    {
        names[0] = "Go";
        names[1] = "Mediterranean Avenue";
        names[2] = "Community Chest";
        names[3] = "Baltic Avenue";
        names[4] = "Income Tax";
        names[5] = "Reading Railroad";
        names[6] = "Oriental Avenue";
        names[7] = "Chance";
        names[8] = "Vermont Avenue";
        names[9] = "Connecticut Avenue";
        names[10] = "In Jail/Just Visiting";
        names[11] = "St. Charles Place";
        names[12] = "Electric Company";
        names[13] = "States Avenue";
        names[14] = "Virginia Avenue";
        names[15] = "Pennsylvania Railroad";
        names[16] = "St. James Place";
        names[17] = "Community Chest(2)";
        names[18] = "Tennessee Avenue";
        names[19] = "New York Avenue";
        names[20] = "Free Parking";
        names[21] = "Kentucky Avenue";
        names[22] = "Chance(2)";
        names[23] = "Indiana Avenue";
        names[24] = "Illinois Avenue";
        names[25] = "B&O Railroad";
        names[26] = "Atlantic Avenue";
        names[27] = "Ventnor Avenue";
        names[28] = "Water Works";
        names[29] = "Marvin Gardens";
        names[30] = "Go to Jail";
        names[31] = "Pacific Avenue";
        names[32] = "North Carolina Avenue";
        names[33] = "Community Chest(3)";
        names[34] = "Pennsylvania Avenue";
        names[35] = "Short Line";
        names[36] = "Chance(3)";
        names[37] = "Park Place";
        names[38] = "Luxury Tax";
        names[39] = "Boardwalk";
    }
    public double[] analyze()
    {
        int[] visits = new int[39];

        for(int i = numPlayers; i < 0; i--)
        {
            int currentPosition = 0;
            int aroundBoard = 1;

            for(int j = timesAround; j > 0; j--)
            {
                Dice rolling = new Dice();
                int number = rolling.roll2Dice();
                currentPosition += number;
                visits[currentPosition]++;

                if(currentPosition == 30)
                {
                    aroundBoard++;
                    currentPosition = 10;
                }

                if(currentPosition > 39)
                {
                    aroundBoard++;
                    currentPosition -= 40;
                }
            }
        }

        double[] percentages = new double[39];
        int totalMoves = 0;

        for(int i = 0; i < 40; i++)
        {
            totalMoves = totalMoves + visits[i];
        }

        for(int i = 0; i < 40; i++)
        {
            percentages[i] = (visits[i] / totalMoves) * 100;
        }

        return percentages;

    }

    public void printResults(double percents[])
    {

        System.out.println("Results of Test:");
        System.out.println("");
        for(int i = 0; i < 40; i++)
        {
            System.out.format(names[i] + "%.2f%n", percents[i]);
        }
    }
}

If you need to see my dice class or my test class, please ask. Any help?

You're initializing visits[] and percentages[] to have 39 elements. This means they will have indices of 0 through 38. Each of your for-loops iterate over the indices 0 through 39. So, 39 is going to cause an issue. Remember, the last index of a zero-based array, is the size minus one. With high-level languages, it is generally good practice to use the length in a for-loop when iterating through an entire array, instead of explicit values, like so:

int[] array = new int[40];
for(int i = 0; i < array.length; i++)
{
    //do something with array[i];
}

This helps prevent a program from going outside array bounds.

Also, your program will never go into the loop in line 55. Your numPlayers is always greater than 0, but the condition checks if it is less than 0 to be true. You need to change it to be greater than...

More... Line 53, you initialize visits array to size of 39, but your whole board consists of 40 slots. Your analysis will be completely wrong.

More more... Line 65 must be moved. This is one of a special case. If the currentPosition value is not greater than 40, then you would need to increment the visited count to that position. However, if it is equal to or greater than 40, you must adjust the value before you record it. As of now, you will run into ArrayOutOfBoundException again.

More more more... Why do lines 84~87 exist??? You should already know the total move number. If 100,000 players move 25 times each, how many steps in total that the whole board will be visited? Why do you still need to calculate the sum by going through the loop?

Edited 4 Years Ago by Taywin

hi, can you please share your code with me?

thanks in advance,
Padaki8@gmail.com

This article has been dead for over six months. Start a new discussion instead.