开发者

How to read a GRID of data from a text file in Java?

开发者 https://www.devze.com 2023-02-17 21:02 出处:网络
When I say a grid, I mean a multidimensional array. I want this because I am making a 2D game and I want to be able to load levels from data text files. Lets say, for example, I have this 2D array lev

When I say a grid, I mean a multidimensional array. I want this because I am making a 2D game and I want to be able to load levels from data text files. Lets say, for example, I have this 2D array level[3][3]. A simple 3x3 map. And I also have a text file that reads:

1 2 3 
4 5 6
7 8 9

In c++, I can simply do:

for (x=0; i<map_width; x++)
{
    for (y=0; y<map_height; y++)
    {
        fscanf(nameoffile, "%d", map[x][y]);
    }
}

And that would put all the contents of the text file accordingly into the array. HOWEVER I have no idea how to do this in java. Is there any sort of equivalent that will just place the data into the array accordingly? I already know about the scanner class, but I don't know how to use it. I have searched google, to no avail. It doesn't explain much. Please help! Specifically, I want to know how to scan the file and put whatever int it reads IN THE APPROPRIATE PLACE in the array.

My current code is this, however, it throws a NoSuchElementException:

public void loadMap() {
    Scanner sc = null;
    try {
        sc = new Scanner(inputmap);
    } catch (Fil开发者_高级运维eNotFoundException e) {
        e.printStackTrace();
    }
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            map[x][y] = sc.nextInt();
        }
    }

Where inputmap is the file, map[][] is a grid of data for each of the tiles on the map and width and height are pre-specified in a constructor.


Your question is very unhelpful when it comes to how your text files will be actually formatted. For example,

123 
456
789

is very different from

1 2 3
4 5 6
7 8 9

and furthermore, you haven't mentioned whether they are always ints, or

1 2 3
4 5 6
a b c

etc. If you gave us a precise description of exactly what goes in these text files we could help you more. The best I can do is show you how to use Scanner to input stuff in general:

The for loop would look similar in Java, but you have to initialize a Scanner object.

Scanner input = new Scanner(myFile); //whatever file is being read

for (x=0; i<map_width; x++)
{
    for (y=0; y<map_height; y++)
    {
        int nextTile = input.nextInt(); //reads the next int
        // or
        char nextTile = input.nextChar(); //reads the next char
    }
}

Beyond that, I would need to know more about what is actually inside these input files.

EDIT:

I copied your for loop directly from your code, but you may want to swap the inner and outer for loops. Shouldn't the width be the inner parameter (reading left to right)?


In Java, it works similar - create a java.util.Scanner object for your file and use it's nextInt method instead of fscanf.


If you don't know the dimensions of the grid

    static int[][] readFile(String filename) {
    try {
        File textfile = new File (GridSearchTest.class.classLoader.getResource(filename).toURI());
        Scanner fileScanner = new Scanner(textfile);
        int size = Integer.parseInt(fileScanner.next());
        String line = fileScanner.nextLine();
        int[][] grid = new int [size][size];
        int i = 0;  int j = 0;

        while (fileScanner.hasNextLine()) {
            line = fileScanner.nextLine();
            System.out.println (line);
            Scanner lineScanner = new Scanner(line);
            while (lineScanner.hasNext()) {
                grid[i][j] = Integer.parseInt(lineScanner.next());
                i++;
            }
            lineScanner.close();
            i=0;
            j++;
        }
        fileScanner.close();

        return grid;

    } catch (IOException e) {
        System.out.println("Error reading file: "+ e.getMessage());
        System.exit(0);
    };
}
0

精彩评论

暂无评论...
验证码 换一张
取 消