Splitting a string array gives me array index out of bounds

441 views Asked by At

Okay, I am trying to generate a tile map with this code. However, I keep on getting an array index out of bounds. So, how this works is that for the "path" I add in a text file. It holds different numbers each representing its own tile texture. The first 2 numbers of the text file is the width and height of it in which we use. What this for loop is doing is assigning each array of tiles[x][y] to a tile in a position where it belongs. Here is the text file I am using:

15 5

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

(there is not extra spaces between the lines idk why it did that)

if there is anything i need to clear up let me know

 String textFile = TextUtility.loadTextAsString(path);


    String[] tileValue = textFile.split("\\s+");

    width = TextUtility.parseToInt(tileValue[0]);
    height = TextUtility.parseToInt(tileValue[1]);

     System.out.println(width+" "+height + " " + tileValue.length);
    tiles = new int[width][height];


    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {

            tiles[x][y] = TextUtility.parseToInt(tileValue[(x+y*(width))+2]);
            System.out.print(""+ tileValue[(x+ y*(width))+2]);
        }
    }
2

There are 2 answers

0
Shyam Baitmangalkar On

The IndexOutOfBounds is due to (x+ y*(width))+2 expression. But if you are just trying to hold each tile's value in tile[][], there is a simpler way in which it can be done!!

I'm assuming that your loadTextAsString(path) is somewhat like this:

public static String loadTextAsString(String path) {
        StringBuilder builder = new StringBuilder();
        try (BufferedReader fileReader = Files.newBufferedReader(Paths.get(path))) {
            String eachLine = "";
            while ((eachLine = fileReader.readLine()) != null) {
                builder.append(eachLine);
                builder.append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }

This returns the textual representation of your file like shown in below example:

15 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

Now, let's start with actual method that will put all these values in a 2-D array.

public int[][] createTiles(String path){
        String textFile = loadTextAsString(path);

        //Get all individual lines in an array
        String[] allLinesInFile = textFile.split("\\n|\\r");

        int width = Integer.parseInt(allLinesInFile[0].split("\\s")[0]);
        int height = Integer.parseInt(allLinesInFile[0].split("\\s")[1]);

        System.out.println("Width -> " + width);
        System.out.println("Height -> " + height);

        //2-D array to hold the tiles
        int[][] tiles = new int[height][width];

        //Row count for the array
        int row = 0;
        for(String eachLine : allLinesInFile){
            String[] allTiles = eachLine.split("\\s");
        /*
         * This will ignore the very first line of the file with width and 
         * height and new line characters
         *
         */
            if(allTiles.length != width){
                continue;
            }

            //Column count for the array
            int col = 0;
            for(String eachTile : allTiles){

                tiles[row][col] = Integer.parseInt(eachTile);
               // Increment column
                col++;
            }
            // Increment Row
            row++;
        }
        //Return the 2-D array.
        return tiles;
    }

I hope this is what you were trying to achieve.

Note: I hope your TextUtility.parseToInt(String val) method is equivalent to Integer.parseInt(String val), hence I've used the later.

2
Remy Cilia On

You're input height is 5 but you only have 4 lines of tiles.