Filling a 2D array from a text file using the Java scanner

advertisements

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;
    }
}