When I execute my program it gives an error :
java.lang.ArrayIndexOutOfBoundsException: 6
at Field.numNeighbours(GameOfLife.java:131)
at Field.generationNextCalc(GameOfLife.java:164)
at GameOfLife.play(GameOfLife.java:67)
at GameOfLife.main(GameOfLife.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
I really don't see it and I hope you can help me.
My code:
import java.util.*; import java.io.*; class GameOfLife{ // main class Field field = new Field(); Scanner scanner; File source; String filename = "GameOfLifeInput.txt"; char[] charArray; public static void main(String[]args){ // starts up program new GameOfLife().play(); } // end of method void readInitial(String filename){ // reads input from file try{ source = new File(filename); scanner = new Scanner(source); String line = null; int row = 0; int col = 0; LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename))); lnr.skip(Long.MAX_VALUE); lnr.getLineNumber(); Cell[][] cells = new Cell[lnr.getLineNumber()][]; while (scanner.hasNext()){ line = scanner.nextLine(); char[] charArray = line.toCharArray(); cells[row] = new Cell[charArray.length]; for (Character c : charArray){ Cell cell = new Cell(); if (c == ' '){ cell.setAlive(false); }else if (c == '*'){ cell.setAlive(true); } cells[row][col] = cell; ++col; } ++row; col = 0; field.setField(cells); } //field.setField(cells); }catch(FileNotFoundException e){ System.out.println("Could not find or open file due to \n"+e); }catch (IOException e) {} } // end of method void play(){ //Asks user for number of generations and execute those generations with brakes of 2 secs in between them try{ int numberOfGenerations = 0; int i = 0; scanner = new Scanner(System.in); System.out.println("How many generations do you want to see?"); numberOfGenerations = scanner.nextInt(); readInitial(filename); for(i = 0; i < numberOfGenerations; i++){ field.print(); field.generationNextCalc(); Thread.sleep(2000); } }catch(InterruptedException e){ } } } // end of class class Field{ int i, j; Cell[][] cells; void setField(Cell[][] cells){ // method which replaces the empty matrix with the matrix with values read from file this.cells = cells; } void drawLine(){ //method to draw lines String s = "+"; for(int i = 1; i < cells[0].length-1; i++){ s+="--"; } s+="+"; System.out.println(s); } // end of method public void print(){ //method to print generations drawLine(); for(int i = 1; i < cells.length-1; i++){ System.out.print("|"); for(int j = 1; j < cells[i].length-1; j++){ if(cells[i][j].isAlive()){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println("|"); } drawLine(); } // end of method int numNeighbours(int i, int j){ // method to count the number of neighbors int result = 0; for(i = 0 ; i<cells[0].length ; i++){ for(j = 0 ; j<cells.length ; j++){ if(i>0 /*&& j>0 && j<cells.length-1*/){ if(cells[i-1][j].alive = true){ // Not top and not left and not right result++; } } if(i<cells[0].length-1 /*&& j>0 && j<cells.length-1*/){ // Not bottem and not left and not right if(cells[i+1][j].alive = true){ result++; } } if(j>0 /*&& i>0 && i<cells[0].length-1*/){ // Not left and not top and not bottem if(cells[i][j-1].alive = true){ result++; } } if(j<cells.length-1 /*&& i>0 && i<cells[0].length-1*/){ // Not right and not top and not bottem if(cells[i][j+1].alive = true){ result++; } } if(i > 0 && j > 0){ // If cell isn't on the left border and top border if(cells[i-1][j-1].alive = true){ result++; } } if(i > 0 && j < cells.length-1){ // If cell isn't on the right border and top border if(cells[i-1][j+1].alive = true){ result++; } } if(i < cells[0].length-1 && j > 0){ // If cell isn't on the bottem border and not on left border if(cells[i+1][j-1].alive = true){ result++; } } if(i < cells[0].length-1 && j < cells.length-1){ // If the cell isn't on the right border and the bottem border if(cells[i+1][j+1].alive = true){ result++; } } }} return result; } // end of method void generationNextCalc(){ // method to count the next generation for (int i = 0; i < cells[0].length-1; i++){ for (int j = 0; j < cells.length-1; j++){ int numNeighbours = numNeighbours(i,j); if (numNeighbours < 2 || numNeighbours > 3){ cells[i][j].setAlive(false); }else if (numNeighbours == 3){ cells[i][j].setAlive(true); } } } /* (int j = 1; j < cells.length-1; j++){ for (int i = 1; i < cells[0].length-1; i++){ cells[i][j] = cells[i][j]; } }*/ } // end of method } // end of class class Cell{ boolean alive = false; GameOfLife character = new GameOfLife(); boolean isAlive(){ return alive; } // end of method void setAlive(boolean state){ alive = state; } // end of method } // end of class