Wes Wes - 2 months ago 18
Java Question

Printing the contents of a two-dimensional array

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(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)


Anyone know what's going on here and how I can correct this??

(This is what is in the sampleMaze.txt file) What I need for it to do is print like this:

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

Answer

If you want to store the width/height in the file, that method could work with a little tweaking. You could make the first two lines of the file contain the width and height of the maze, and use parseInt(file.nextLine()) instead of parseInt(file.next()). An input file could look like this:

12
9
############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#

and the code, instead of

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());

A much better way to get the width and height would be to determine them from the file's actual dimensions (Note: including the dimensions at the top of the input file for this method would mess things up). Instead of passing in a Scanner to Maze's constructor, pass in the File itself. Then, set width = new Scanner(file).nextLine().length() and height = file.length(), where file isn't the Scanner, but the File itself. Then set scanner = new Scanner(file) and use that for the rest of the method.