Java 将文件中的字符逐行读取到二维数组中

Java 将文件中的字符逐行读取到二维数组中,java,arrays,Java,Arrays,我在将字符从文件放入2d数组时遇到问题。当我打印出数组时,我得到了null,我不知道为什么 程序读入一个文件,格式为第一行包含2个数字,分别对应于高度和宽度,它首先检查命令行参数是否有效。如果是,它将读取文件,根据这两个数字创建二维数组,然后将文件的其余部分存储到数组中。文件的其余部分是一个由字符组成的迷宫,如下所示: 2 3 +-+-+-+ |S| | + + + + | |E| +-+-+-+ 因此,2d阵列模拟了迷宫结构。方法readMazeFile是我正在努力的地方 impor

我在将字符从文件放入2d数组时遇到问题。当我打印出数组时,我得到了
null
,我不知道为什么

程序读入一个文件,格式为第一行包含2个数字,分别对应于高度和宽度,它首先检查命令行参数是否有效。如果是,它将读取文件,根据这两个数字创建二维数组,然后将文件的其余部分存储到数组中。文件的其余部分是一个由字符组成的迷宫,如下所示:

2 3
+-+-+-+
|S|   |
+ + + +
|   |E|
+-+-+-+
因此,2d阵列模拟了迷宫结构。方法
readMazeFile
是我正在努力的地方

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class MazeSolver {

   // The name of the file describing the maze
   static String mazefile;
   static int width;
   static int height;
   static int arrayWidth;
   static int arrayHeight;
   static char[][] mazeArrays;   
   public static void main(String[] args) throws FileNotFoundException {
      if (handleArguments(args)) {

         readMazeFile(mazefile, arrayHeight, arrayWidth);
         System.out.println(Arrays.deepToString(mazeArrays));
         //DrawMaze.draw(mazeArrays, height, width);

         if (solveMaze())
            System.out.println("Solved!");
         else
            System.out.println("Maze has no solution.");
      }
      else {
      System.out.println("The arguments are invalid.");
      }
   }

   // Handle the input arguments
   static boolean handleArguments(String[] args) {
      if (args.length > 4 || args.length < 1) {
         System.out.println("There are too many or too few command line arguments");
         return false;
      }
      if (args.length == 1) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         return true;
      }
      if (args.length == 2) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         if (cellsize < 10) {
            return false;
         }
         return true;
      }
      if (args.length == 3) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         if (borderwidth < 5) {
            return false;
         }
         return true;
      }
      if (args.length == 4) {
         mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         int sleeptime = Integer.parseInt(args[3]);
         if (sleeptime < 0 || sleeptime > 10000) {
            return false;
         }
         return true;
      }   
      return false;
   }

   // Read the file describing the maze.
   static void readMazeFile(String mazefile, int arrayHeight, int arrayWidth) throws FileNotFoundException {

      Scanner scanner = new Scanner(new File(mazefile));
      height = scanner.nextInt();
      width = scanner.nextInt();
      arrayHeight = 2 * height + 1;
      arrayWidth = 2 * width + 1;
      char[][] mazeArrays = new char[arrayHeight][arrayWidth];
      while (scanner.hasNextLine()) {
         String line = scanner.nextLine();
         System.out.println(line);
         for (int row = 0; row < arrayHeight; row++) {
           for (int col = 0; col < line.length(); col++) {
               mazeArrays[row][col] = line.charAt(col);




           }
         }



      }

   }

   // Solve the maze.      
   static boolean solveMaze() {
      return true;
   }
}

谢谢你的帮助,我为业余爱好者的缺陷道歉,我正在努力学习Java。我想把它保留在这个结构中。在此之后,我必须使用2d数组以递归方式设置迷宫解决方案的动画。

从readMazeFile返回数组,并将其分配到与以下相同的数组中:

mazeArrays = readMazeFile(mazefile, arrayHeight, arrayWidth);
将返回类型更改为char[][]

static char[][] readMazeFile(String mazefile, int arrayHeight, int arrayWidth) throws FileNotFoundException

您将得到null,因为您将mazarrays作为类变量和局部变量,所以值已更新为局部变量而不是类变量。因此,您需要返回Mazarrays数组,并在main方法中为Mazarrays赋值。

我认为您的问题在于这一行:

    char[][] mazeArrays = new char[arrayHeight][arrayWidth];
基本上,当您以后修改
mazarrays
变量时,您将覆盖此局部变量,而不是方法外部的静态变量。删除
char[][]
将解决您的问题

(像这样)

    char[][] mazeArrays = new char[arrayHeight][arrayWidth];
    mazeArrays = new char[arrayHeight][arrayWidth];