Hi all -
I need to read a text file using a scanner class that has the following format:

5 5
5 5 5 5 5
0 0 0 0 5
5 5 0 5 5
5 0 0 0 5
5 5 5 0 0
2 0

The first line is to be read in and used to determine the size of the 2D array.
The first line determines the size of the maze as well. The program is than to skip the first line, and read in the next 5 lines into a 2D array. It is to skip the last line, as those ar the starting coordinates for the ma

I keep getting an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at programfour.ArrayReader.main(ArrayReader.java:28) error. Any advice?

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class ArrayReader {

    public static void main(String[] args) throws FileNotFoundException{

    File inputFile = new File("maze1.txt");
    Scanner sc = new Scanner(inputFile);
    int i = 0;
    int j = 0;
    int count = 1;
    int arrayRowSize = sc.nextInt();
    int arrayColumnSize = sc.nextInt();
    int[][] mazeArray = new int[arrayRowSize][arrayColumnSize];
    sc.nextLine();

    while(sc.hasNextInt()){
        if(count % arrayRowSize == 0){
            mazeArray[i][j] = sc.nextInt();
            count++;
            i++;
            j = 0;
        } else {
            mazeArray[i][j++] = sc.nextInt();
            count++;
        }

    }


    }
}

Recommended Answers

All 5 Replies

ArrayIndexOutOfBoundsException: 6
at programfour.ArrayReader.main(ArrayReader.java:28)

The index used on line 28 is past the end of the array.
What are the dimensions of the array?
Which index is past the end?
Remember that the max value for an index is the array length-1.
An array defined with 5 elements can be indexed with the values 0 to 4

Try debugging the code by printing out the values of the variables:(i,j,count) used in the code so you can see what the code is doing.

Hi ThisIsMeOrIsIt, you have sc.hasNextInt() as the while loop condition, which continues even after you have read ur 5X5 data from your file, and at line #26 the program ends up assigning '2' at location mazeArray[5][0] which is obviously out of bounds (remember you have array indices 0 to 4?).

Try using following code:

for(int i=0; i<arrayRowSize; i++){
            for(int j=0; j< arrayColumnSize; j++){
                mazeArray[i][j] = sc.nextInt();
            }
            sc.nextLine();
}

Or, if you find using while loop absolutely necessary, put 2 more conditions inside while loop which check value of i and j are <=4.

Hope that helps.

That did work..however, the output of the code is printing it all on one line..5555500005550555000555500...how would I make it print out like it was read in?
AKA:

55555
00005
55055
50005
55500

I figured it out..

for(int i = 0; i < mazeArray.length; i++){
        for(int j = 0; j < mazeArray[i].length; j++){
            System.out.print(mazeArray[i][j] + " ");
        }
        System.out.println();
    }

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.