打印二维数组的内容

时间:2014-10-10 22:54:58

标签: java arrays 2d

该程序的目的是从txt文件中读取字符并将它们存储到2D数组中。完成此操作后,将以与从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文件中的内容)我需要做的就是这样打印:

############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#

2 个答案:

答案 0 :(得分: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)并将其用于方法的其余部分。

答案 1 :(得分:0)

最简单的方法:

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