0

I have the following code. I am using a 2D array and placing a number, either 2 or 1 in it. For some reason, I'm getting an error on line 84 for ArrayIndexOutOfBoundsException. Line 84 is family[addToArray][count1] = 1;

public static void main(String[] args) {
    int count = 0; // count will be number of times it took to get a girl, aka the amount of boys
    int[][] family = new int[1000000][];
    Random rn = new Random();
    Family f = new Family();
    
    int answer = rn.nextInt(2) + 1;

    // 1 is girl, 2 is boy
    int count2 = 1;

    boolean check = f.step(answer);

    try {
        // if step is true then add to array
        // isnt adding to the family because i need to make a growing variable the number
        int addToArray = 0;
        int count1, count3 = 0;
        while (count2 < 1000000) {
            count = 0;
            count3 = 0;
            if (check) {
                family[addToArray] = new int[1];
                family[addToArray][0] = 1;
            }
            else {
                // keep creating new children until the new kid is a girl, add to array using count until you get a girl
                answer = rn.nextInt(2) + 1;
                check = f.step(answer);
                count = count + 1;
                while (!check) {
                    answer = rn.nextInt(2) + 1;
                    check = f.step(answer);
                    // count is number of boys
                    count = count + 1;

                }

                count1 = count;

                // here count will be amount of boys and girls in the family, create array with that many columns
                family[addToArray] = new int[count];
                for (int e = 0; e < count; e++) {
                        while (count3 < count) {
                            family[addToArray][count3] = count1;
                            count3++;
                            
                        }
                }
                family[addToArray][count1] = 1;

            }
            count2 = count2 + 1;    
            addToArray = addToArray + 1;
            answer = rn.nextInt(2) + 1;
            check = f.step(answer);
            count = 0;
        }       
    
    } catch (NullPointerException npe) {
        System.out.println("oh god please no");
    }
    catch (ArrayIndexOutOfBoundsException aioobe) {
        System.out.println("Nooooooooooo!");
    }


    // System.out.println(Arrays.toString(family));
    System.out.println(f.getGirls());
    System.out.println(f.getBoys());

    double num = f.getGirls() + f.getBoys();
    double percentGirls = f.getGirls() / num;
    System.out.println(percentGirls);   
}
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • 1
    It's quite hard to follow the code, being a pretty long method and with variables without meaningful names, like "count", "count1", "count2", "count3". Have you tried running this in a debugger and breaking when the exception is thrown? What are the values of `addToArray` and `count1` at that point? How large is `family[addToArray]` at that point? (I'd strongly advise trying to split your method into smaller methods, and give everything meaningful names... it'll make life a lot easier.) – Jon Skeet Aug 28 '20 at 17:51
  • I'll try to test out the values throughout, thanks for the suggestion! –  Aug 28 '20 at 18:02

0 Answers0