2014-10-10 124 views
3

該程序的要點是讀取txt文件中的字符並將它們存儲到二維數組中。完成此操作後,將以與從txt文件中讀取相同的方式打印信息。打印二維數組的內容

這裏是我的代碼至今:

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

public class Main { 

    public static void main(String[] args) throws FileNotFoundException { 
     File file = new File("sampleMaze.txt"); 
     Scanner s = new Scanner(file); 
     Maze maze = new Maze(s); 
     System.out.print(file); 
    } 
} 

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

public class Maze { 

    public int width; 
    public int height; 
    public Square [] [] sampleMaze; 

    Maze(Scanner file) { 
     this.width = Integer.parseInt(file.next()); 
     this.height = Integer.parseInt(file.next()); 
     this.sampleMaze = new Square [height] [width]; 

     for (int i = 0 ; i < height ; i++) { 
      String s = file.next(); 

      for (int j = 0 ; j < width ; j++) { 
       sampleMaze[height][width] = Square.fromChar(s.charAt(j)); 
      } 

     } 
     System.out.print(sampleMaze[height][width]); 
    } 

} 

public enum Square { 
    WALLS("#"), 
    OPEN_SPACES("."), 
    START("o"), 
    FINISH("*"); 

    String x; 

    Square(String x) { 
     this.x = x; 
    } 

    public String toString() { 
     return x; 
    } 

    public static Square fromChar(char x) { 
     if (x == '#') 
      return WALLS; 
     else if (x == '.') 
      return OPEN_SPACES; 
     else if (x == 'o') 
      return START; 
     else if (x == '*') 
      return FINISH; 
     else 
      throw new IllegalArgumentException(); 
    } 
} 

這是努力實現項目的目標,當我收到錯誤:

Exception in thread "main" java.lang.NumberFormatException: For input string: "############" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at Maze.<init>(Maze.java:15) 
    at Main.main(Main.java:20) 

任何人都知道這是怎麼回事在這裏以及我如何糾正這個?

(這是在sampleMaze.txt文件),我需要爲它做的是打印這樣的:

​​
+0

當你分析你的寬度和高度則trowing NumberFormatException異常整型,這意味着不管它是從文件中讀取心不是要分析 – JRowan 2014-10-10 23:01:59

+0

更具體的int,它是字符串「############」,表明輸入文件中缺少寬度和高度。 – 2014-10-10 23:02:59

+0

所以我應該寫這樣的代碼: this.width = Character.parseChar(file.next()); this.height = Character.parseChar(file.next()); – Wes 2014-10-10 23:04:06

回答

1

如果你想寬度/高度存儲在文件中,該方法可以稍微調整一下。您可以使文件的前兩行包含迷宮的寬度和高度,並使用parseInt(file.nextLine())而不是parseInt(file.next())。輸入文件看起來是這樣的:

12 
9 
############ 
#.#........# 
#.#.######.# 
#.#....#...# 
#.###.*#.#.# 
#...####.#.# 
#.#.#..#.#.# 
#.#.#.##.#.# 
#o#......#.# 

和代碼,而不是

this.width = Integer.parseInt(file.next()); 
this.height = Integer.parseInt(file.next()); 

this.width = Integer.parseInt(file.nextLine()); 
this.height = Integer.parseInt(file.nextLine()); 

一個更好的方式來獲得的寬度和高度將從文件的實際尺寸中確定它們(注意:包括此方法輸入文件頂部的尺寸會導致混亂)。不要將掃描器傳遞給Maze的構造函數,而是傳入文件本身。然後,設置width = new Scanner(file).nextLine().length()height = file.length(),其中file不是掃描儀,而是文件本身。然後設置scanner = new Scanner(file)並將其用於該方法的其餘部分。

+0

因此,我將自己的構造函數更改爲您提到的方式,但對於您最後一段中要說的內容,我確實感到困惑。在學習編程方面,我很新,所以我正試圖找出完成此任務的最簡單方法。 – Wes 2014-10-10 23:48:35

+0

我可以說更好。爲了獲得寬度,你創建一個新的文件掃描器,並獲取文件的第一行。然後,您只需獲取該字符串的長度。要獲得高度,請調用File的length()方法,該方法返回文件中的行數 – wgoodall01 2014-10-11 01:26:14

0

最簡單的方法:

 Console.WriteLine("Array : "); 
     Console.WriteLine("[{0}]", string.Join(", ", <your array>));