Question

My program complies but when I run the program it gives me an "Array Index Out of Bounds Exception"

public void readBoard(String filename) throws Exception { 
    File f = new File("myBoard.csv"); 
    Scanner reader = new Scanner(f); 
    while(reader.hasNext()){ 
        String line = reader.nextLine(); 
        String [] b = line.split(","); 
        String type = b[0]; 
        int i = Integer.parseInt(b[1]); 
        int j = Integer.parseInt(b[2]); 
        if(type.equals("Chute")) 
            board[i][j] = new Chute(); 
        else if(type.equals("Ladder")) 
            board[i][j] = new Ladder(); 
}

the error is at int i = Integer.parseInt(b[1]); My question is the way I turned the String [1] and [2] into an int correct? I would think it's not since I have an array out of bounds exception. I'm guessing it means it point at the spot in the area and there's nothing.

Was it helpful?

Solution

IndexOutOfBounds indeed means that you're trying to access an element which does not exist in your array. Add:

System.out.println("array size = " + b.length);

to see how long the array actually is. You expect your array to have a length of 3, but based on the actual line read and the manner in which you split it, you appear to have an array of length 1. It would also help to see the actual line that you're trying to split.

Try this:

public void readBoard(String filename) throws Exception{
    File f = new File("myBoard.csv");
    Scanner reader = new Scanner(f);
    while(reader.hasNext()){
        String line = reader.nextLine();

        // What line do we intend to process?

        System.out.println("Line = " + line);

        String [] b = line.split(",");

        // How long is the array?

        System.out.println("Array length = " + b.length);

        String type = b[0];
        int i = Integer.parseInt(b[1]);
        int j = Integer.parseInt(b[2]);
        if(type.equals("Chute"))
            board[i][j] = new Chute();
        else if(type.equals("Ladder"))
            board[i][j] = new Ladder();
    }

Whenever you're in the process of developing code, you want to add debug statements that dump the value of various fields to help you see what you're doing. Sprinkling your code with a few key debug statements here and there will help you reconcile your assumptions (i.e. "my array has three elements") with what's actually going on (i.e. "my array only has one element").

OTHER TIPS

Make sure line splitting works fine and you have 3 distinct strings o.w. b[1] or b[2] should lead to error. Take a print or debug to see what is the value of b[0].

try this it, since it was out of bound due to the array size is 1 you should skip all the arrays with size of 1.

public void readBoard(String filename) throws Exception {
    File in = new File("myBoard.csv");
    Scanner reader = new Scanner(in);
    while (reader.hasNext()) {
        String line = reader.nextLine();
        String[] b = line.split(",");
        if (b.length != 1) {
            String type = b[0];
            int i = Integer.parseInt(b[1]);
            int j = Integer.parseInt(b[2]);
            if (type.equals("Chute"))
                board[i][j] = new Chute();
            else if (type.equals("Ladder"))
                board[i][j] = new Ladder();
        }
    }
}

Before the while loop do this

reader.nextLine[];

Because the first line of the file only has one element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top