gpt4 book ai didi

java - 康威生活游戏中的项目不匹配异常

转载 作者:行者123 更新时间:2023-11-30 08:55:31 24 4
gpt4 key购买 nike

我正在为类编写康威生命游戏程序。下面的方法读取指定的输入文件,将其读入一个 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:对于输入字符串:“000000000000000000011100000000000000000000000000000000000000000000000000000000”在 java.util.Scanner.nextInt(未知来源)

在 java.util.Scanner.nextInt(未知来源)

在 Proj5.readBoard(Proj5.java:30)

在 Proj5.main(Proj5.java:8)

而且我不完全确定为什么或如何修复它...

最佳答案

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

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

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

关于java - 康威生活游戏中的项目不匹配异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28954213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com