有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用scanner Java从文本文件填充二维数组

我试图通过使用扫描仪读取文本文件来填充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游戏创建地形

所以文本文件-->;二维阵列-->;地形生成


共 (2) 个答案

  1. # 1 楼答案

    如果您的目标是与现有阵列进行比较,那么我就不用麻烦从文件中创建另一个二维阵列了。相反,我只是在读取文件时逐行检查

    这种方法有一个好处,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.
    }
    
  2. # 2 楼答案

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