The point of this program is to read characters from a txt file and store them into a 2D array. After this has been accomplished, the information is to be printed in the same manner it is read from in the txt file.
Here is the code I have so far:
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();
}
}
And this is the error I am receiving when trying to accomplish the goal of the project:
Exception in thread "main" java.lang.NumberFormatException: For input string: "############"
at java.lang.NumberFormatException.forInputString(Unk nown 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)
Anyone know what's going on here and how I can correct this??
The information I am trying to store in a 2D array and then print is this:
############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#