简体   繁体   English

使用扫描程序Java从文本文件填充2D数组

[英]Populating 2D array from text file using scanner Java

I am trying to populate a 2D array in Java through reading a text file using scanner. 我试图通过使用扫描仪读取文本文件来填充Java中的2D数组。 Currently, I don't fully understand what's going wrong. 目前,我还不完全了解出了什么问题。 Below are the text file I'm using, the 2D array when it is printed and my code. 下面是我正在使用的文本文件,打印时的2D数组和我的代码。

Text File 文本文件

30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 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

Printed Array 印刷阵列

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]]

Currently, my code reads every integer in a while loop. 目前,我的代码读取while循环中的每个整数。 Whilst the x-coordinate (The x position in the array) increases up to 19 in the array, it is set to zero and the y-coordinate increases (The y position in the array). 虽然x坐标(数组中的x位置)在数组中增加到19,但它被设置为零并且y坐标增加(数组中的y位置)。 This is the method I used to populate the array. 这是我用来填充数组的方法。

Note: The width and height of 2D array (mapWidth and mapHeight) and text file are both 20 * 20. Additionally, the initial value of xCoord and yCoord are 0. 注意:2D数组(mapWidth和mapHeight)和文本文件的宽度和高度均为20 * 20.此外,xCoord和yCoord的初始值为0。

My Code 我的守则

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

Any help would be appreciated! 任何帮助,将不胜感激!

EDIT 编辑

I want to create a 2D array that matches the text file so I can later reference the values of the 2D array easily in a sprite sheet to create terrain for a 2D game. 我想创建一个与文本文件匹配的2D数组,以便稍后可以在精灵表中轻松地引用2D数组的值,以便为2D游戏创建地形。

So Text File --> 2D array --> Terrain Generation 所以文本文件 - > 2D数组 - >地形生成

You are missing some integers because you do next twice: fileScannerTileMap.nextInt(); 这是因为丢失了一些整数,因为你做next两次: fileScannerTileMap.nextInt(); and fileScannerTileMap.next(); fileScannerTileMap.next();

Try that: 试试看:

if (fileScannerTileMap.hasNextInt()) {
    tileMap[xCoord][yCoord] = fileScannerTileMap.nextInt();
} else {
    fileScannerTileMap.next();
}

And break the loop when you are done: 完成后打破循环:

if (mapWidth - 1 == xCoord) { 
    yCoord++;
    xCoord = 0;

    // add this:
    if (mapWidth - 1 == yCoord) { 
        break;
    }
}

You can use a little trick for calculating the row and column numbers: 您可以使用一个小技巧来计算行数和列数:

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] + " ");
            }
        }
    }
}

I wrote this code in a way that you can just copy-paste and try yourself, but actually you only need the loop for reading the values. 我以一种你可以复制粘贴并尝试自己的方式编写这段代码,但实际上你只需要循环来读取值。

Output: 输出:

30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 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 

If your objective is to compare with an existing array, I wouldn't bother creating another 2D array from the file . 如果您的目标是与现有阵列进行比较,我不会费心从该文件创建另一个2D阵列 Instead, I'd just check row by row as and when I read from the file. 相反,当我从文件中读取时,我只是逐行检查。

This approach has a benefit, readAllLines will close the file handle in case of a NumberFormatException or any other exception. 这种方法有一个好处, readAllLines将在NumberFormatException或任何其他异常的情况下关闭文件句柄。

Let's say int[][] myArray is the original array that you wan't to compare with and the file is located at "filepath" . 假设int[][] myArray是你想要比较的原始数组,文件位于"filepath" The boolean function compareWithMyArray will return true if your file matches the 2D array. 如果您的文件与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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM