康威生命游戏中的物品不匹配例外

时间:2015-03-10 00:31:15

标签: java

我正在为一个康威的生命游戏程序进行上课..下面的方法读取一个指定的输入文件,将其读入一个int数组,并返回该数组..

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;
}

扫描程序读取的文本文件的格式如下所示..其中顶行是行数,第二行是列数,其余是需要放入数组的数据:

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

然而,当我正在运行我的程序时...我正在接受:

线程“main”中的异常java.util.InputMismatchException:对于输入字符串:“00000000000000000001110000000000000000000000000000000000000000000000000000000” 在java.util.Scanner.nextInt(未知来源)

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

在Proj5.readBoard(Proj5.java:30)

在Proj5.main(Proj5.java:8)

并且我不完全确定为什么或如何解决它......

1 个答案:

答案 0 :(得分:2)

使用nextInt()将尝试将每个文件行转换为单个整数。

抛出异常,因为被视为单个数字的整行不符合int值。

我要解决此问题的方法是每行使用一次readLine次调用来消耗整行输入。然后使用charAt String方法检查该行特定列的字符。如有必要,可以通过减去int的值有效地将其转换为'0'