Excepción fuera de los límites del índice de matriz usando Integer.parseInt

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.

preguntado el 25 de abril de 13 a las 00:04

4 Respuestas

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.

Prueba esto:

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").

Respondido 25 Abr '13, 00:04

Ohhhhhhhhh, this helped! Thank you. Printing out the size of the array was helpful. It proved the array was a size of 1 and not 3. Now I see where the problem is, in the file the method is calling. Thank you so much! - Marcella Ruiz

Happy to be of service. Sprinkling is fun and helps your code grow. This is the most basic of debugging techniques and will serve you as long as you continue coding. Just remember to remove the debug statements in your release version, or you can always wrap debug statements. Create a final static boolean variable in your class called DEBUG, then when you need to sprinkle, just use if(DEBUG) System.out.println("myVar" + myVar); - Marteatómico

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].

Respondido 25 Abr '13, 00:04

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

Respondido 25 Abr '13, 02:04

Before the while loop do this

reader.nextLine[];

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

Respondido 26 Abr '13, 22:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.