2015-02-10 79 views
0

我無法將字符從文件中放入2d數組中。當我打印出陣列時,我得到了null,我不知道爲什麼。將字符從文件逐行讀入到2d數組

程序讀入一個文件,格式化爲第一行,包含2個數字,分別對應於高度和寬度,它首先檢查命令行參數是否有效。如果是,則讀取該文件,根據這兩個數字創建一個2d數組,然後將該文件的其餘部分存儲到該數組中。該文件的其餘部分是字符的迷宮,像這樣:

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

這裏是輸出:

+-+-+-+ 
|S| | 
+ + + + 
| |E| 
+-+-+-+ 
null 
Solved! 

感謝您的幫助,我對任何業餘瑕疵道歉,我想學習Java。我想保持這種結構。在此之後,我必須使用2d數組來通過遞歸對迷宮的解決方案進行動畫製作。從readMazeFile

回答

0

返回的數組併爲它分配在同一個陣列:

mazeArrays = readMazeFile(mazefile, arrayHeight, arrayWidth); 

CHAGE返回類型爲char [] []

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

,因爲你有mazeArrays爲類你得到空變量以及局部變量,因此值已更新爲局部變量而非類變量。所以你需要返回mazeArray數組,並且你已經在main方法中使用了mazeArrays。

0

我覺得你的問題是這一行:

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

基本上,當你以後修改mazeArrays變量要覆蓋此局部變量,而不是你的方法以外的靜態變量。刪除char[][]將解決您的問題。

(像這樣)

mazeArrays = new char[arrayHeight][arrayWidth];