使用扫描仪从文本文件填充二维数组 Java

Populating 2D array from text file using scanner Java

我正在尝试通过使用扫描仪读取文本文件来填充 Java 中的二维数组。目前,我不完全明白出了什么问题。下面是我正在使用的文本文件、打印时的二维数组和我的代码。

文本文件

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 位置)。这是我用来填充数组的方法。

注意:二维数组(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 数组,这样我以后可以在 sprite 中轻松引用 2D 数组的值 sheet 来为 2D 游戏创建地形。

所以文本文件 --> 二维数组 --> 地形生成

您缺少一些整数,因为您执行了两次 nextfileScannerTileMap.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;
    }
}

如果您的 objective 是为了 与现有数组进行比较,我不会费心从文件 创建另一个二维数组。相反,我只是在读取文件时逐行检查。

这种方法有一个好处,readAllLines 将在 NumberFormatException 或任何其他异常情况下关闭文件句柄。

假设 int[][] myArray 是您不想与之比较的原始数组,文件位于 "filepath"。如果您的文件与二维数组匹配,boolean 函数 compareWithMyArray 将 return 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.
}

你可以使用一个小技巧来计算行号和列号:

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