2014-10-10 138 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>));