Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Game of life counter not working

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Game of life counter not working

    I'm running into an issue where my method that should count neighbors gets an array out of bounds error. I have tried a couple of things to fix it, but they've all failed. Any suggestions?

    public static int neighbors(boolean[][] matrix, int row, int col) {
    		int count = 0;
     
    		if (matrix[row][col]) {
    			count++;
    		}
    		if (matrix[row - 1][col]) {
    			count++;
    		}
    		if (matrix[row + 1][col]) {
    			count++;
    		}
    		if (matrix[row][col - 1]) {
    			count++;
    		}
    		if (matrix[row][col + 1]) {
    			count++;
    		}
    		if (matrix[row + 1][col + 1]) {
    			count++;
    		}
    		if (matrix[row + -1][col - 1]) {
    			count++;
    		}
    		if (matrix[row - 1][col + 1]) {
    			count++;
    		}
    		if (matrix[row + 1][col - 1]) {
    			count++;
    		}
     
     
    		return count;
     
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Game of life counter not working

    gets an array out of bounds error.
    Can you copy the full text of the error message and paste it here? It should show the line number of the statement and the value of the index.

    Also post the code that shows the dimensions of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Game of life counter not working

    This is the error:
    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at Life.neighbors(Life.java:51)
    at Life.main(Life.java:29)"

    The matrix is determined by the user input:
    boolean[][] matrix = new boolean[userRows][userColumns];

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Game of life counter not working

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at Life.neighbors(Life.java:51)
    The statement at line 51 used an index (8) that was past the end of the array. The array had less than 9 elements.
    What statement is at line 51? What variable had the invalid value?
    Remember that the valid indexes for an array range in value from 0 to the size-1. An index of 8 requires at least 9 elements in the array.

    The code that checks the slots at the boundaries (top, right, bottom and left) of the rectangle must be careful to not go over the boundary.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Conway's game of Life please help D:
    By geeksutopia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 10th, 2013, 01:24 PM
  2. Anyone know how to do this game of life problem
    By by513 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2013, 10:19 PM
  3. Need help with Game of Life generations
    By MarcusSt0ne in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 7th, 2012, 08:56 AM
  4. Help with my game of life program
    By thatni**a in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 11th, 2012, 09:20 AM
  5. Can't run Game of Life programm
    By cutekill0 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 13th, 2011, 08:30 AM

Tags for this Thread