- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为类编写康威生命游戏程序。下面的方法读取指定的输入文件,将其读入一个 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
调用来消耗整行输入。然后使用 String
的 charAt
方法检查该行特定列的字符。如有必要,可以通过减去 '0'
的值将其有效地转换为 int
。
关于java - 康威生活游戏中的项目不匹配异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28954213/
我正在尝试使用 Java6(JAR) 解决以下关于 spoj 的问题:- 你的程序是使用蛮力方法来找到生命、宇宙和一切的答案。更准确地说......从输入到输出重写小数字。读入数字42后停止处理输入。
我是一名优秀的程序员,十分优秀!