简体   繁体   中英

Populating 2D array from text file using scanner Java

I am trying to populate a 2D array in Java through reading a text file using scanner. Currently, I don't fully understand what's going wrong. Below are the text file I'm using, the 2D array when it is printed and my code.

Text File

30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 28 22 23 29 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 28 22 33 9 10 34 23 29 30 30 30 30 30 30 
30 30 30 30 28 22 33 9 17 1 1 18 10 34 23 29 30 30 30 30 
30 30 28 22 33 9 17 1 1 1 1 1 1 18 10 34 23 29 30 30 
28 22 33 9 17 1 1 1 1 1 1 1 1 1 1 18 10 34 23 29 
33 9 17 1 1 2 1 1 1 1 1 1 1 1 6 1 1 18 10 34 
32 8 16 1 1 1 1 1 1 1 1 1 1 1 1 5 1 15 7 31 
27 25 32 8 16 1 1 3 1 1 1 1 1 1 1 15 7 31 24 26 
30 30 27 25 32 8 16 1 1 1 1 1 1 15 7 31 24 26 30 30 
30 30 30 30 27 25 32 8 16 1 1 15 7 31 24 26 30 30 30 30 
30 30 30 30 30 30 27 25 32 8 7 31 24 26 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 27 25 24 26 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30

Printed Array

Map array: [[30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 6, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 30, 1, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 28, 10, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 32, 24, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 16, 30, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 17, 1, 1, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 1, 1, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 23, 1, 1, 1, 27, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 1, 1, 1, 32, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 10, 1, 1, 7, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 30, 23, 1, 7, 24, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 30, 28, 33, 7, 24, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 28, 33, 17, 27, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 33, 17, 1, 32, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 10, 1, 1, 16, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, 30, 23, 1, 1, 1, 27, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

Currently, my code reads every integer in a while loop. Whilst the x-coordinate (The x position in the array) increases up to 19 in the array, it is set to zero and the y-coordinate increases (The y position in the array). This is the method I used to populate the array.

Note: The width and height of 2D array (mapWidth and mapHeight) and text file are both 20 * 20. Additionally, the initial value of xCoord and yCoord are 0.

My Code

while (fileScannerTileMap.hasNext()) {

    if (fileScannerTileMap.hasNextInt()) {
        tileMap[xCoord][yCoord] = fileScannerTileMap.nextInt();
    }

    fileScannerTileMap.next();

    // Increase xCoord each time
    xCoord++;

    // When xCoord reaches its max at 19, increase yCoord and set xCoord equal to zero
    if (mapWidth - 1 == xCoord) { 
        yCoord++;
        xCoord = 0;
    }
}

Any help would be appreciated!

EDIT

I want to create a 2D array that matches the text file so I can later reference the values of the 2D array easily in a sprite sheet to create terrain for a 2D game.

So Text File --> 2D array --> Terrain Generation

You are missing some integers because you do next twice: fileScannerTileMap.nextInt(); and fileScannerTileMap.next();

Try that:

if (fileScannerTileMap.hasNextInt()) {
    tileMap[xCoord][yCoord] = fileScannerTileMap.nextInt();
} else {
    fileScannerTileMap.next();
}

And break the loop when you are done:

if (mapWidth - 1 == xCoord) { 
    yCoord++;
    xCoord = 0;

    // add this:
    if (mapWidth - 1 == yCoord) { 
        break;
    }
}

You can use a little trick for calculating the row and column numbers:

public class Example {

    public static void main(String[] args) {

        // Initialize the scanner
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File("map.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // Reading the values
        int size = 20;
        int[][] map = new int[size][size];
        for (int i = 0; scanner.hasNextInt(); i++) {
            map[i/size][i%size] = scanner.nextInt();
        }

        // Print the array
        for (int i = 0; i < size; i++) {
            System.out.println();
            for (int j = 0; j < size; j++) {
                System.out.print(map[i][j] + " ");
            }
        }
    }
}

I wrote this code in a way that you can just copy-paste and try yourself, but actually you only need the loop for reading the values.

Output:

30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 28 22 23 29 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 28 22 33 9 10 34 23 29 30 30 30 30 30 30 
30 30 30 30 28 22 33 9 17 1 1 18 10 34 23 29 30 30 30 30 
30 30 28 22 33 9 17 1 1 1 1 1 1 18 10 34 23 29 30 30 
28 22 33 9 17 1 1 1 1 1 1 1 1 1 1 18 10 34 23 29 
33 9 17 1 1 2 1 1 1 1 1 1 1 1 6 1 1 18 10 34 
32 8 16 1 1 1 1 1 1 1 1 1 1 1 1 5 1 15 7 31 
27 25 32 8 16 1 1 3 1 1 1 1 1 1 1 15 7 31 24 26 
30 30 27 25 32 8 16 1 1 1 1 1 1 15 7 31 24 26 30 30 
30 30 30 30 27 25 32 8 16 1 1 15 7 31 24 26 30 30 30 30 
30 30 30 30 30 30 27 25 32 8 7 31 24 26 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 27 25 24 26 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 

If your objective is to compare with an existing array, I wouldn't bother creating another 2D array from the file . Instead, I'd just check row by row as and when I read from the file.

This approach has a benefit, readAllLines will close the file handle in case of a NumberFormatException or any other exception.

Let's say int[][] myArray is the original array that you wan't to compare with and the file is located at "filepath" . The boolean function compareWithMyArray will return true if your file matches the 2D array.

boolean compareFileWithMyArray(String filepath, int[][] myArray) {
    List<String> lines = Files.readAllLines(Paths.get("filepath")); // Each line is stored as a String in lines.

    int row = 0; //Keeps track of the current row in the 2D array
    for(String str : lines)
    {
        String[] arr = str.split(" "); //The String array now contains each number in the given line as a String
        if(arr.length != myArray[row].length) // Before checking if numbers match, checking lengths is a easy weed out.
            return false;
        for(int i = 0; i < arr.length; i++) // Check if individual numbers match.
            if(Integer.parseInt(arr[i]) != myArray[row][i])
                return false;
        row++; //Move on to the next row if the previous row was a match.
    }

    if(row != lines.size()) // The file could've been a subset of the 2D array, so you want to be sure that all rows matched.
        return false;

    return true; // If all the above tests pass, your file and the 2D array have identical values.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM