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