java - Array out of bounds exception? (Working on conway's game of life) -
java - Array out of bounds exception? (Working on conway's game of life) -
so i'm halfway through first java class... might need break babe steps me, apologize!
i'm working on conway's game of life right now. luckily not using gui (...yet?) i'm still stuck on setting grid. code compiling fine, when run get: error: not find or load main class gameoflife so, looked more , seems issue grid file, gives same error: error: not find or load main class grid
edit**: how running them
[bgorgero@sraysvcs2 gameoflife]$ java gameoflife error: not find or load main class gameoflife [bgorgero@sraysvcs2 gameoflife]$ java grid error: not find or load main class grid
edit*:
[bgorgero@sraysvcs2 ~]$ java gameoflife/gameoflife exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 @ gameoflife.gameoflife.main(gameoflife.java:20)
that error when seek access without beingness in folder.
i'm trying grid read input txt file find out how big array should be, , read 0's , 1's , store them array. far, code
gameoflife.java
package gameoflife; import java.util.scanner; import java.io.file; import java.io.printwriter; import java.io.filenotfoundexception; public class gameoflife{ public static void main (string[] args) throws exception { file input = new file("gameboardinput.txt"); scanner reader = new scanner(input); int x = reader.nextint(); int y = reader.nextint(); grid grid = new grid(x,y); for(int = 0; < x; i++){ string cupcake = reader.nextline(); char[] muffin = cupcake.tochararray(); for(int j = 0; j < y; j++){ grid.setat(i,j,muffin[j]); } } } }
grid.java
package gameoflife; public class grid{ protected int[][] grid; public grid (int x, int y){ grid = new int[x][y]; (int = 0; < x; i++){ (int j = 0; j < y; j++){ grid[i][j] = 0; } } } public int nuggets (int x, int y){ homecoming grid[x][y]; } public void setat(int column, int row, int alive){ grid[column][row] = alive; } }
my gameboardinput.txt simply:
5 6 01000 00100 00010 01110 00100 00000
thank guys!
if class in package gameoflife;
need have class in directory called gameoflife
then, run with:
$ java gameoflife/gameoflife
make sure that, based on code provided, gameboardinput.txt
in same directory. got run different error on computer:
$ ls . gameoflife/ .: gameboardinput.txt gameoflife gameoflife/: gameoflife.class grid.class $ java gameoflife/gameoflife exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 @ gameoflife.gameoflife.main(gameoflife.java:20)
as aside, should never name classes lowercase letter. recommend gameoflife
class instead. read more here: java naming conventions
you have other problems:
you using bad variable names. utilize improve namescupcake
, muffin
, ones mean variable is. e.g. utilize nextline
instead of cupcake
, linechars
instead of muffin
. you attempting parse empty lines. simplest way prepare add together loop skip empty lines: code:
string cupcake = reader.nextline(); while(cupcake.isempty()) cupcake = reader.nextline();
you have x
, y
swapped in loop. way loop works, y
should first number , x
should sec number, e.g. code:
int y = reader.nextint(); int x = reader.nextint();
here's exact code:
gameoflife/gameoflife.javapackage gameoflife; import java.util.scanner; import java.io.file; import java.io.printwriter; import java.io.filenotfoundexception; public class gameoflife { public static void main(string[] args) throws exception { file input = new file("gameboardinput.txt"); scanner reader = new scanner(input); int y = reader.nextint(); int x = reader.nextint(); grid grid = new grid(x, y); (int = 0; < x; i++) { string cupcake = reader.nextline(); while (cupcake.isempty()) cupcake = reader.nextline(); char[] muffin = cupcake.tochararray(); (int j = 0; j < y; j++) { grid.setat(i, j, muffin[j]); } } } }
gameoflife/grid.java package gameoflife; public class grid { protected int[][] grid; public grid(int x, int y) { grid = new int[x][y]; (int = 0; < x; i++) { (int j = 0; j < y; j++) { grid[i][j] = 0; } } } public int nuggets(int x, int y) { homecoming grid[x][y]; } public void setat(int column, int row, int alive) { grid[column][row] = alive; } }
gameboardinput.txt 5 6 01000 00100 00010 01110 00100 00000
java arrays conways-game-of-life
Comments
Post a Comment