Printing the contents of a two-dimensional array

The point of this program is to read characters from a txt file and save them to a 2D array. After that, the information should be printed in the same way as it is read from a 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 get when trying to execute the project target:

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)

      

Does anyone know what is going on here and how can I fix it ??

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

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

      

+3


source to share


2 answers


If you want to keep the width / height in the file, this method can work with a little tweak. You can make the first two lines of the file contain the maze width and height and use parseInt(file.nextLine())

instead parseInt(file.next())

. The input file might look like this:

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

      

and the code instead of

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

      



will be

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

      

Your best bet is to get the width and height to determine from the actual file dimensions (Note: including the dimensions at the top of the input file for this method will be messed up). Instead of passing the constructor Maze

in the scanner, go through the file itself. Then install width = new Scanner(file).nextLine().length()

and height = file.length()

, where is file

not a scanner, but the file itself. Then install scanner = new Scanner(file)

and use that for the rest of the method.

+1


source


The easiest way:



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

      

0


source







All Articles