我试图通过使用扫描仪读取文本文件来填充Java中的2D数组。目前,我还不完全了解出了什么问题。下面是我正在使用的文本文件,打印时的2D数组和我的代码。
文字档案
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 28 22 23 29 30 30 30 30 30 30 30 30
30 30 30 30 30 30 28 22 33 9 10 34 23 29 30 30 30 30 30 30
30 30 30 30 28 22 33 9 17 1 1 18 10 34 23 29 30 30 30 30
30 30 28 22 33 9 17 1 1 1 1 1 1 18 10 34 23 29 30 30
28 22 33 9 17 1 1 1 1 1 1 1 1 1 1 18 10 34 23 29
33 9 17 1 1 2 1 1 1 1 1 1 1 1 6 1 1 18 10 34
32 8 16 1 1 1 1 1 1 1 1 1 1 1 1 5 1 15 7 31
27 25 32 8 16 1 1 3 1 1 1 1 1 1 1 15 7 31 24 26
30 30 27 25 32 8 16 1 1 1 1 1 1 15 7 31 24 26 30 30
30 30 30 30 27 25 32 8 16 1 1 15 7 31 24 26 30 30 30 30
30 30 30 30 30 30 27 25 32 8 7 31 24 26 30 30 30 30 30 30
30 30 30 30 30 30 30 30 27 25 24 26 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
印刷数组
Map array: [[30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 6, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 30, 1, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 28, 10, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 32, 24, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 16, 30, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 17, 1, 1, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 23, 1, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 7, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 33, 17, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 1, 1, 1, 27, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
目前,我的代码读取while循环中的每个整数。虽然x坐标(数组中的x位置)在数组中增加到19,但它被设置为零并且y坐标增加(数组中的y位置)。这是我用来填充数组的方法。
注意:2D数组(mapWidth和mapHeight)和文本文件的宽度和高度均为20 * 20.此外,xCoord和yCoord的初始值为0。
我的代码
while (fileScannerTileMap.hasNext()) {
if (fileScannerTileMap.hasNextInt()) {
tileMap[xCoord][yCoord] = fileScannerTileMap.nextInt();
}
fileScannerTileMap.next();
// Increase xCoord each time
xCoord++;
// When xCoord reaches its max at 19, increase yCoord and set xCoord equal to zero
if (mapWidth - 1 == xCoord) {
yCoord++;
xCoord = 0;
}
}
任何帮助将不胜感激!
编辑
我想创建一个与文本文件匹配的2D数组,以便稍后可以在精灵表中轻松引用2D数组的值,为2D游戏创建地形。
所以文本文件 - > 2D阵列 - >地形生成
答案 0 :(得分:2)
您错过了一些整数,因为您执行了next
两次:fileScannerTileMap.nextInt();
和fileScannerTileMap.next();
试试:
if (fileScannerTileMap.hasNextInt()) {
tileMap[xCoord][yCoord] = fileScannerTileMap.nextInt();
} else {
fileScannerTileMap.next();
}
完成后打破循环:
if (mapWidth - 1 == xCoord) {
yCoord++;
xCoord = 0;
// add this:
if (mapWidth - 1 == yCoord) {
break;
}
}
答案 1 :(得分:1)
您可以使用一个小技巧来计算行数和列数:
public class Example {
public static void main(String[] args) {
// Initialize the scanner
Scanner scanner = null;
try {
scanner = new Scanner(new File("map.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Reading the values
int size = 20;
int[][] map = new int[size][size];
for (int i = 0; scanner.hasNextInt(); i++) {
map[i/size][i%size] = scanner.nextInt();
}
// Print the array
for (int i = 0; i < size; i++) {
System.out.println();
for (int j = 0; j < size; j++) {
System.out.print(map[i][j] + " ");
}
}
}
}
我以一种你可以复制粘贴并尝试自己的方式编写这段代码,但实际上你只需要循环来读取值。
输出:
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 28 22 23 29 30 30 30 30 30 30 30 30
30 30 30 30 30 30 28 22 33 9 10 34 23 29 30 30 30 30 30 30
30 30 30 30 28 22 33 9 17 1 1 18 10 34 23 29 30 30 30 30
30 30 28 22 33 9 17 1 1 1 1 1 1 18 10 34 23 29 30 30
28 22 33 9 17 1 1 1 1 1 1 1 1 1 1 18 10 34 23 29
33 9 17 1 1 2 1 1 1 1 1 1 1 1 6 1 1 18 10 34
32 8 16 1 1 1 1 1 1 1 1 1 1 1 1 5 1 15 7 31
27 25 32 8 16 1 1 3 1 1 1 1 1 1 1 15 7 31 24 26
30 30 27 25 32 8 16 1 1 1 1 1 1 15 7 31 24 26 30 30
30 30 30 30 27 25 32 8 16 1 1 15 7 31 24 26 30 30 30 30
30 30 30 30 30 30 27 25 32 8 7 31 24 26 30 30 30 30 30 30
30 30 30 30 30 30 30 30 27 25 24 26 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
答案 2 :(得分:0)
如果你的目标是与现有阵列进行比较,我就不会在文件中创建另一个2D阵列了。相反,当我从文件中读取时,我只是逐行检查。
这种方法有一个好处,readAllLines
会在NumberFormatException
或任何其他例外的情况下关闭文件句柄。
我们说int[][] myArray
是您要与之比较的原始数组,文件位于"filepath"
。如果您的文件与2D数组匹配,boolean
函数compareWithMyArray
将返回true
。
boolean compareFileWithMyArray(String filepath, int[][] myArray) {
List<String> lines = Files.readAllLines(Paths.get("filepath")); // Each line is stored as a String in lines.
int row = 0; //Keeps track of the current row in the 2D array
for(String str : lines)
{
String[] arr = str.split(" "); //The String array now contains each number in the given line as a String
if(arr.length != myArray[row].length) // Before checking if numbers match, checking lengths is a easy weed out.
return false;
for(int i = 0; i < arr.length; i++) // Check if individual numbers match.
if(Integer.parseInt(arr[i]) != myArray[row][i])
return false;
row++; //Move on to the next row if the previous row was a match.
}
if(row != lines.size()) // The file could've been a subset of the 2D array, so you want to be sure that all rows matched.
return false;
return true; // If all the above tests pass, your file and the 2D array have identical values.
}