打印二维数组的内容

3
这个程序的目的是从文本文件中读取字符并将其存储到二维数组中。完成后,信息将以与读取文本文件相同的方式打印出来。
以下是我目前的代码:
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#......#.#

当您解析宽度和高度的整数时,它会抛出NumberFormatException异常,这意味着它从文件中读取的内容不是可解析的整数。 - JRowan
更具体地说,它是字符串“############”,表明输入文件中缺少宽度和高度信息。 - Patricia Shanahan
所以我应该这样编写代码:this.width = Character.parseChar(file.next()); this.height = Character.parseChar(file.next()); - Wes
你应该发布txt文件的内容,另外你的for循环在每个周期都填充相同的数组维度,我认为这会让你出现问题。 - JRowan
正如异常明确指出的那样,您正在尝试将字符串“############”转换为int,从而引发了NumberFormatException。请发布您的txt文件以实际查看您正在从中读取什么。 - Paco Lagunas
我已经编辑了主贴并添加了txt文件的内容,我需要将其存储在2D数组中,然后打印出来。 - Wes
2个回答

1
如果您想将宽度/高度存储在文件中,那么这种方法需要进行一些微调即可。您可以使文件的前两行包含迷宫的宽度和高度,并使用parseInt(file.nextLine())代替parseInt(file.next())。输入文件可能如下所示:
12
9
############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#

而代码,而不是
this.width = Integer.parseInt(file.next());
this.height = Integer.parseInt(file.next());

would be

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

更好的方法是从文件的实际尺寸确定宽度和高度(注意:在输入文件顶部包括尺寸会使这种方法变得混乱)。不要传递一个Scanner到的构造函数,而应该传递文件本身。然后,设置width = new Scanner(file).nextLine().length()height = file.length(),其中file不是Scanner,而是文件本身。然后设置scanner = new Scanner(file)并将其用于方法的其余部分。

所以我按照你提到的方式更改了我的构造函数,但是老实说,我对你在最后一段中试图表达的意思感到困惑。当涉及学习编程时,我还是比较新手,因此我正在尝试找出完成此任务的最简单方法。 - Wes
我本可以用更好的措辞表达。要获取宽度,您需要创建一个新的Scanner对象来读取文件,并获取文件的第一行。然后,您只需获取该字符串的长度即可。要获取高度,请调用File的length()方法,该方法返回文件中的行数。 - wgoodall01

0

最简单的方法:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接