Hey folks,

Basically, I am trying to recreate Conway's Game of Life in java. I'm getting input from a file and using it as the first generation. I set up my array and have input working correctly. However, when I'm trying to use a method evaluate my array at certain indexes to determine whether the point is alive or not, it doesn't seem to recognize the array declared earlier in the same class. I'm sure this is very simple, but if anybody can tell me how to correct this it would be much appreciated.

Each of the 8 'if' statements in the last method produce an error, where b[][] cannot be found in the class life....

import java.util.*;
import java.io.*;


public class life{
    public static void main(String[] args) throws IOException {

        java.io.File file = new java.io.File("desktop/input.txt");

        Scanner input = new Scanner(file);

        try {

            int rows= input.nextInt();
            int columns = input.nextInt();



            char[][] b = new char[rows][columns];
            char[][] temp = new char[rows][columns];

            for (int i=0; i< rows; i++) {
                String aLine = input.next();
                for (int j=0; j<columns; j++) {

                b[i][j]=aLine.charAt(j);

                if (j == (columns-1)) 

                System.out.println(b[i][j]);

                else
                System.out.print(b[i][j]);


                    }
                }
            }   







        catch (Exception e) {


        }

    }

    public static int getAlive( int i, int j ) {

            int result = 0;

            try { 

                if (b[(i-1)][(j-1)] == 'X') 

                    result ++;



                if (b[(i-1)][(j)] == 'X') 

                    result ++;



                if (b[(i-1)][(j+1)] == 'X') 

                    result ++;



                if (b[(i)][(j-1)] == 'X') 

                    result ++;



                if (b[(i)][(j+1)] == 'X') 

                        result ++;



                if (b[(i+1)][(j-1)] == 'X') 

                        result ++;



                if (b[(i+1)][(j)] == 'X') 

                        result ++;



                if (b[(i+1)][(j+1)] == 'X') 

                        result ++;

                    return result;

            }

    catch (RuntimeException ex){

        }
    }
}

because b[][] isnt defined within the scope of the getAlive() method.....

 import java.util.*;
 import java.io.*;

 public class life{
 char[][] b;

    public static void main(String[] args) throws IOException {

     java.io.File file = new java.io.File("desktop/input.txt");
     Scanner input = new Scanner(file);
     try {
         int rows= input.nextInt();
         int columns = input.nextInt();
         b = new char[rows][columns];
         char[][] temp = new char[rows][columns];
         for (int i=0; i< rows; i++) {
             String aLine = input.next();
             for (int j=0; j<columns; j++) {
             b[i][j]=aLine.charAt(j);
             if (j == (columns-1)) 
             System.out.println(b[i][j]);
             else
             System.out.print(b[i][j]);
                 }
             }
         }   
     catch (Exception e) {
     }


    }
}

or alternatively instead of defining b[][] as a global variable then just pass the array to the getAlive() method?

import java.util.*;
import java.io.*;
public class life{
char[][] b;
public static void main(String[] args) throws IOException {
java.io.File file = new java.io.File("desktop/input.txt");
Scanner input = new Scanner(file);
try {
int rows= input.nextInt();
int columns = input.nextInt();
b = new char[rows][columns];
char[][] temp = new char[rows][columns];
for (int i=0; i< rows; i++) {
String aLine = input.next();
for (int j=0; j<columns; j++) {
b[i][j]=aLine.charAt(j);
if (j == (columns-1))
System.out.println(b[i][j]);
else
System.out.print(b[i][j]);
}
}
}
catch (Exception e) {
}
}
}

Thanks for the response. It now says that b cannot be accessed by the main method since it is not static.

then make variable b static by adding the modifier/keyword 'static' before the declaration of b like so:

static char[][] b;
catch (RuntimeException ex){
    }

You should never, and I do mean never, yes, really never never never ever do this in a new piece of code. If an error happens you will never see the error message and will never know what happened. Always start with an ex.printStackTrace(); in the catch block.

commented: very good advice +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.