Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用scanner Java从文本文件填充二维数组_Java_Arrays_Multidimensional Array_Java.util.scanner - Fatal编程技术网

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

使用scanner Java从文本文件填充二维数组,java,arrays,multidimensional-array,java.util.scanner,Java,Arrays,Multidimensional Array,Java.util.scanner,我试图通过使用扫描仪读取文本文件来填充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 3

我试图通过使用扫描仪读取文本文件来填充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
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;
    }
}
打印阵列

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

我的代码

30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 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
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数组-->Terrain Generation

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

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

这种方法有一个好处,
readAllLines
将在出现
NumberFormatException
或任何其他异常时关闭文件句柄

假设
int[][]myArray
是您不想比较的原始数组,文件位于
的“文件路径”
。如果您的文件与2D数组匹配,则
布尔值
函数
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.
}
boolean compareFileWithMyArray(字符串文件路径,int[]myArray){
List lines=Files.readAllLines(path.get(“filepath”);//每行作为字符串存储在行中。
int row=0;//跟踪二维数组中的当前行
用于(字符串str:lines)
{
String[]arr=str.split(“”;//字符串数组现在将给定行中的每个数字作为字符串包含
if(arr.length!=myArray[row].length)//在检查数字是否匹配之前,检查长度是一个很容易的方法。
返回false;
for(int i=0;i
您可以使用一个小技巧来计算行数和列数:

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

预期的输出是什么?2D数组的输出应与文本文件相同。(我想用文本文件的数组值替换数组中的相应值)很抱歉没有将其清除@Rossw19,你应该修改你的问题以反映你的真实需要。我似乎得到了一个Bounds的例外,谢谢你的帮助!检查我的更新。如果这不能解决问题,请告诉我们异常发生的确切时间/地点谢谢,它起作用了。我发现我有另一个问题,当我从mapWidth中删除一个时,我意外地在2D数组中使用了错误的索引。在这个问题中,你是从哪里得到比较的想法的?@user1803551,OP在他的一条评论中提到了它。我没有看到,但没问题。