Reputation: 13
So I'm about halfway through my first java class... so you might need to break it into baby steps for me, I apologize!
I'm working on Conway's game of life right now. Luckily we are not using a gui for it (...yet?) but I'm still stuck on simply setting up the grid. My code is compiling fine, but when I run I get: Error: Could not find or load main class gameOfLife So, I looked into it more and it seems that the issue is with my grid file, which gives the same error: Error: Could not find or load main class Grid
EDIT**: this is how I am running them
[bgorgero@sraysvcs2 gameOfLife]$ java gameOfLife
Error: Could not find or load main class gameOfLife
[bgorgero@sraysvcs2 gameOfLife]$ java Grid
Error: Could not find or load main class Grid
EDIT*:
[bgorgero@sraysvcs2 ~]$ java gameOfLife/gameOfLife
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at gameOfLife.gameOfLife.main(gameOfLife.java:20)
That is the error I get when I try to access it without being in the folder.
I'm trying to get the grid to read from the input txt file to find out how large the array should be, and then read the 0's and 1's and store them into the array. So far, this is my 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 i = 0; i < 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];
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
grid[i][j] = 0;
}
}
}
public int nuggets (int x, int y){
return grid[x][y];
}
public void setAt(int column, int row, int alive){
grid[column][row] = alive;
}
}
my gameBoardInput.txt is simply:
5
6
01000
00100
00010
01110
00100
00000
Thank you guys!
Upvotes: 0
Views: 545
Reputation: 208
Use a try-catch block in the main method, don't make it throw an exception. What is your JDK version? Try something like:
public static void main(String[] args){
try{
//insert code here
}catch(Exception e){e.printStackTrace();}
Also, use a java.util.ArrayList<String>
to store the input text using its add(<String>)
method. Then get the number of rows using size() and the number of columns using get(0).length()
Also, you have missing code. The main method does not have 20 lines and Exception occurs at line 20. It's probably due to a out-of-bounds condition in your code for calculating the neighbours, but we need the rest of the code to work that out. Please edit accordingly.
Upvotes: 0
Reputation: 32343
If your class is in package gameOfLife;
You need to have the class in a directory called gameOfLife
Then, run it with:
$ java gameOfLife/gameOfLife
Make sure that, based on the code you provided, the gameBoardInput.txt
is in the same directory. I got it to run with a different error on my computer:
$ ls . gameOfLife/
.:
gameBoardInput.txt gameOfLife
gameOfLife/:
gameOfLife.class Grid.class
$ java gameOfLife/gameOfLife
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at gameOfLife.gameOfLife.main(gameOfLife.java:20)
As an aside, you should never name classes with a lowercase letter. I recommend GameOfLife
for the class instead. Read more here: Java Naming Conventions
You have some other problems:
cupcake
and muffin
, ones that actually mean what the variable is. e.g. use nextLine
instead of cupcake
and lineChars
instead of muffin
.Code:
String cupcake = reader.nextLine();
while(cupcake.isEmpty()) cupcake = reader.nextLine();
x
and y
swapped in your loop. The way your loop works, y
should be the first number and x
should be the second number, e.g.Code:
int y = reader.nextInt();
int x = reader.nextInt();
Here's my exact code:
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 y = reader.nextInt();
int x = reader.nextInt();
Grid grid = new Grid(x, y);
for (int i = 0; i < x; i++) {
String cupcake = reader.nextLine();
while (cupcake.isEmpty())
cupcake = reader.nextLine();
char[] muffin = cupcake.toCharArray();
for (int j = 0; j < y; j++) {
grid.setAt(i, j, muffin[j]);
}
}
}
}
package gameOfLife;
public class Grid {
protected int[][] grid;
public Grid(int x, int y) {
grid = new int[x][y];
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
grid[i][j] = 0;
}
}
}
public int nuggets(int x, int y) {
return grid[x][y];
}
public void setAt(int column, int row, int alive) {
grid[column][row] = alive;
}
}
5
6
01000
00100
00010
01110
00100
00000
Upvotes: 2