lundi 9 mars 2015

Item Mismatch Exception in conways game of life



I'm working on a conway's game of life program for class.. the method below reads a specified input file, reads it into an int array, and returns that array..



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

public class Proj5 {
public static void main(String[] args) throws IOException {
//the main method controls the flow of the program
String filename = args[0];
int[][] cells = readBoard(filename);

for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
System.out.print(cells[i][j]);
}
System.out.println("");
}

}

public static int[][] readBoard(String filename) throws IOException {
// This method reads the specified input file, reads it into an int[][] array, and returns that array.
Scanner inFile = new Scanner(new File(filename));
int row = Integer.parseInt(inFile.nextLine());
int col = Integer.parseInt(inFile.nextLine());

int[][] cells = new int[row][col];

inFile.nextLine();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cells[i][j] = inFile.nextInt();
}
inFile.nextLine();
}

inFile.close();
return cells;
}


And the text file that the scanner reads is formatted like below.. where the top line is the number of rows, second line is the number of columns, and the rest is the data that needs to be put into the array:



25
77
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000001110000000000000000000000000000000000000000000000000000000
00000000000000001110001110000000000000000000000000111110000000000000000000000
00000000000000111100000111100000000000000000001111100011111000000000000000000
00000000000000001110001110000000000000000000000000111110000000000000000000000
00000000000000000001110000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000001110000000000000000000000000000000000000000000000000000000
00000000000000001110001110000000000000000000000000111110000000000000000000000
00000000000000111100000111100000000000000000001111100011111000000000000000000
00000000000000001110001110000000000000000000000000111110000000000000000000000
00000000000000000001110000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000


However, when i'm running my program.. i'm getting:


Exception in thread "main" java.util.InputMismatchException: for input string: "00000000000000000001110000000000000000000000000000000000000000000000000000000" at java.util.Scanner.nextInt(unknown source)


at java.util.Scanner.nextInt(unknown source)


at Proj5.readBoard(Proj5.java:30)


at Proj5.main(Proj5.java:8)


and i'm not entirely sure why or how to fix it...




Aucun commentaire:

Enregistrer un commentaire