Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:20:20+00:00 2026-06-15T17:20:20+00:00

Hello I am writing a program for a programming class and I am getting

  • 0

Hello I am writing a program for a programming class and I am getting a:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
    at Life.countNeighbors(Life.java:200)
    at Life.genNextGrid(Life.java:160)

I’ve gotten ArrayIndexOutOfBoundsException errors before, usually caused when I try to add or substract a index of an array. However this time it is quite different and I was hoping someone here can point out why the error is occuring.

info about my program: the program is like John Conway game of life. using a 2d array and then setting certain elements to (true = alive) or (false = dead) the program then determine if a cell lives or dies in the next generation base on the number of neighbors it has. (3 neighbors = birth of a new cell) (2,3 neighbor = they stay alive) anything else they died in the next generation.

The IndexOutOfBound error is caused by this line according to my editor:

if(!(grid[1][1]) && !(grid[1][18]) && !(grid[18][1]) && !(grid[18][18]))

I created the above line as a constraint, and it should not tell java to search a index beyond the bounds of the original array since they are merely booleans (true/false)statement in the end. If anyone can help me debug this error that would be splendid.

HERE IS MY CODE: (does not include main method)

 public static void clearGrid ( boolean[][] grid )
{
    int col;
    int row = 1;

    while(row < 18){
       for(col = 1; col < 18; col++){
          grid[row][col]= false;//set each row to false
       }
         row++;
    }//set all elements in array to false
}

public static void genNextGrid ( boolean[][] grid )
{
    //new tempprary grid
     boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];

     TempGrid= grid; // copy the current grid to a temporary grid

     int row = 1;
     int col = 1;

    countNeighbors(TempGrid, row, col); // passes the TempGrid to countNieghbor method

 for(row = 1; row < 18; row++){

        countNeighbors(TempGrid, row, col);

        for(col = 1; col < 18; col++){

            countNeighbors(TempGrid, row, col);

            if(countNeighbors(grid, row, col) == 3)
            {
                TempGrid[row][col] = true;
            }
            else if(countNeighbors(grid, row, col) == 2 || countNeighbors(grid, row, col) == 3)
    {
                TempGrid[row][col] = true;
            }
            else
            {
                TempGrid[row][col] = false;
            }

        }
    }
}

public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
{
    int n = 0; //int used to store the # of neighbors
    int temprow = row;
    int tempcol = col;
//count # of neighbors for the cell on the edge but not the corner
            for(temprow = row; temprow <= 18; temprow++)
            {
                for(tempcol = row; tempcol <= 18; tempcol++)
                {
                     if(temprow == 1 || temprow == 18 || tempcol == 1 || tempcol ==18)
                     {
                         if(!(grid[1][1]) && !(grid[1][18]) && !(grid[18][1]) && !(grid[18][18]))
                         {
                             if(grid[temprow][tempcol] == true)
                             {
                                 n++;
                             }
                         }
                     }
                }
            }


//count # of neighbors for the corner cells
     for(temprow = row; temprow <= 18; temprow++)
     {
         for(tempcol = row; tempcol <= 18; tempcol++)
         {
            if(grid[1][1] || grid[1][18] || grid[18][1] || grid[18][18])
                    {
                        if(grid[temprow][tempcol] == true)
            {
                n++;
            }
                    }
         }
     }

// count the cells that are not on the edge or corner
    while(temprow >= 2 && tempcol >= 2 && temprow <= 17 && tempcol <= 17)
    {
    for(temprow = row; temprow-1 <= temprow+1; temprow++)
    {
            for(tempcol = col; tempcol-1 <= tempcol+1; tempcol++)
            {
                if(grid[temprow][tempcol] == true)
                {
                    n++;
                }
            }
    }
    }
    return n; // return the number of neighbors
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:20:21+00:00Added an answer on June 15, 2026 at 5:20 pm

    Without a full stack trace and an indication as to where the problem lies, this is my best guess:

    grid[18][1]
    

    The value 18 is beyond the size of the array you can access, in Java arrays are zero based (0). Since I have seen 17 all throughout your post, this seems like the most logical reason why.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello i am writing a java program to remove all comment from a string
I am writing a simple program: class Demo{ public static void main(String[] args){ system.out.println("Hello");
Hello I am new to programming and I am writing a program in C.
I am writing a Tkinter program. The main class is MainFrame() class MainFrame(Frame): def
I am writing somewhat of a proxy program in Java. Here's how it works:
SOLVED see Edit 2 Hello, I've been writing a Perl program to handle automatic
I am writing a program that will change java code. It changes the next-line
Hello I am writing this program, and I am trying to figure out how
Hello guys I'm writing a test file for a program. where all the possible
Hello i am writing a program and i have declared a string in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.