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 8635655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:01:47+00:00 2026-06-12T10:01:47+00:00

I’m currently in an intro to programming in Java class and I’ve ran into

  • 0

I’m currently in an intro to programming in Java class and I’ve ran into a problem or two.

First off my currently program is “Conway’s Game of Life” I’ve got everything working except for the algorithm to check the neighboring cells.

I’ve checked out about 7 or 8 different post on Stackoverflow and a couple on other sites and so far I can’t find anyone who has taken the approach I have.

Right now all I need is someone to look over my code and see if it should work and if not why not? Currently I’m getting a runtime error that says:

“ArrayIndexOutOfBoundsException: 5 at Life.checkold(Life.java:151)”.

I’ve marked this place in the code the best I could.

My input to this these methods are an array of size 5 by 5

First off, thank you for at least reading this. Second if I need to include anything else please just leave a comment or reply (new here sorry)

Building NextGen:

public static boolean[][] buildnext(boolean[][] lastgen)
{
    boolean[][] nextgen = new boolean[lastgen.length][lastgen[0].length];

    for(int r = 0; r < lastgen[0].length; r++)
    {
        for(int c = 0; c < lastgen.length; c++)
        {
            nextgen[c][r] = checkold(lastgen, c, r);
        }
    }
    return nextgen;

}

My Checking Method:

public static boolean checkold(boolean[][] lastgen, int col, int row)
    {
        int acount = 0;
        boolean alive = lastgen[col][row];

        if(col == 0 && row == 0) //Top Left Corner
        {       
            if(lastgen[col][row + 1] == true) acount++; //Below
            if(lastgen[col + 1][row] == true) acount++; //Right
            if(lastgen[col + 1][row + 1] == true) acount++; //Below Right
        }

        else if(col == lastgen.length && row == 0)//Top Right Corner
        {
            if(lastgen[col][row + 1] == true) acount++; //Below
            if(lastgen[col - 1][row] == true) acount++; //Left
            if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
        }
        else if(col == 0 && row == lastgen[0].length)//Bottom Left Corner
        {
            if(lastgen[col][row - 1] == true) acount++; //Above
            if(lastgen[col + 1][row] == true) acount++; //Right
            if(lastgen[col + 1][row - 1] == true) acount++; //Above Right

        }
        else if(col == lastgen.length && row == lastgen[0].length) //Bottom Right Corner
        {
            if(lastgen[col][row - 1] == true) acount++; //Above
            if(lastgen[col - 1][row] == true) acount++; //Left
            if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
        }
        else if(col == 0 && row > 0 && row < lastgen[0].length) //Left Col
        {
            if(lastgen[col][row - 1] == true) acount++; //Above
                            if(lastgen[col][row + 1] == true) acount++; //Below  (This is the code that the runtime error is about)
            if(lastgen[col + 1][row] == true) acount++; //Right
            if(lastgen[col + 1][row - 1] == true) acount++; //Above Right
            if(lastgen[col + 1][row + 1] == true) acount++; //Below Right

        }
        else if(col == lastgen.length && row > 0 && row < lastgen[0].length) //Right Col
        {
            if(lastgen[col - 1][row] == true) acount++; //Left
            if(lastgen[col][row - 1] == true) acount++; //Above
            if(lastgen[col][row + 1] == true) acount++; //Below
            if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
            if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
        }
        else if(col < 0 && row == 0) //Top Row
        {
            if(lastgen[col][row + 1] == true) acount++; //Below
            if(lastgen[col - 1][row] == true) acount++; //Left
            if(lastgen[col + 1][row] == true) acount++; //Right
            if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
            if(lastgen[col + 1][row + 1] == true) acount++; //Below Right
        }
        else if(col < 0 && row == lastgen[0].length) //Bottom Row
        {
            if(lastgen[col][row - 1] == true) acount++; //Above
            if(lastgen[col - 1][row] == true) acount++; //Left
            if(lastgen[col + 1][row] == true) acount++; //Right
            if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
            if(lastgen[col + 1][row - 1] == true) acount++; //Above Right
        }
        else if(col < 0 && row < 0) //Middle Cells
        { 
            if(lastgen[col][row + 1] == true) acount++; //Below
            if(lastgen[col][row - 1] == true) acount++; //Above
            if(lastgen[col - 1][row] == true) acount++; //Left
            if(lastgen[col + 1][row] == true) acount++; //Right

            if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
            if(lastgen[col + 1][row - 1] == true) acount++; //Above Right
            if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
            if(lastgen[col + 1][row + 1] == true) acount++; //Below Right
        }

        if(acount == 3 && alive == false) alive = true;
        if(acount == 1) alive = false;
        if(acount == 3 && alive == true) alive = false;

        return alive;
    }
  • 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-12T10:01:49+00:00Added an answer on June 12, 2026 at 10:01 am

    your exception is quite obvious, you are trying to accessan array index which is out of bound.

    else if(col == 0 && row > 0 && row < lastgen[0].length) //Left Col
            {
                if(lastgen[col][row - 1] == true) acount++; //Above
                                if(lastgen[col][row + 1] == true) acount++; //Below  (This is the code that the runtime error is about)
    

    your code fails on this line:

    if(lastgen[col][row + 1] == true) 
    

    say lastgen array is 2X3 array and row = 2, else if part would be true, and consider if is also true, now in here

    if(lastgen[col][row + 1] == true) 
    

    youa re trying to access 3 index,
    remember: Array indexes start with zero. if your arrays length is 3, your array index will be 0,1,2. 2 will the last index.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've tracked down a weird MySQL problem to the two different ways I was
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns 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.