Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayIndexOutOfBoundsException for a 2D array
    primarykey
    data
    text
    <p>I am trying to create a program that generates chutes and ladders onto 2D array board that is of size 10X10. In order to generate these chutes and ladders, I had to create a method called <code>readBoard</code> that basically reads a written file (myBoard.csv) and translates the written information into positions and place the chutes and ladders onto the 2D array board accordingly. </p> <p>The program compiles but when I run it there is this error:</p> <p><code>java.lang.ArrayIndexOutOfBoundsException: 1 at ChutesAndLadders.readBoard(ChutesAndLadders.java:41) at TestFile.main(TestFile.java:13) </code> My impression was that perhaps my written file had additional spaces that I didn't see that's why it's telling me I'm out of bound. However, I checked the written file and it looks fine. If you could provide some insight into this problem, that would be great. Thanks in advance! </p> <p>Here is my <code>getBoard</code>method:</p> <pre><code>public void readBoard(String filename)throws Exception { Scanner s=new Scanner(new File(filename)); s.nextInt(); while (s.hasNext()) { String line=s.nextLine(); String[]singleSplit=line.split(","); String cellType=singleSplit[0]; int row=Integer.parseInt(singleSplit[1]); int col=Integer.parseInt(singleSplit[2]); if (cellType.equals("Chute")) board[row][col]=new Chute(); else board[row][col]=new Ladder(); } } </code></pre> <p>and here is my written file (type,row,column):</p> <p>29</p> <p>Chute,1,0</p> <p>Chute,2,0</p> <p>Chute,3,0</p> <p>Chute,4,0</p> <p>Chute,5,0</p> <p>Chute,6,0</p> <p>Chute,7,0</p> <p>Chute,8,0</p> <p>Chute,9,0</p> <p>Chute,0,1</p> <p>Chute,0,2</p> <p>Chute,0,3</p> <p>Chute,9,1</p> <p>Chute,9,2</p> <p>Chute,9,3</p> <p>Ladder,0,5</p> <p>Ladder,1,5</p> <p>Ladder,2,5</p> <p>Ladder,3,5</p> <p>Ladder,4,5</p> <p>Ladder,5,5</p> <p>Ladder,6,5</p> <p>Ladder,7,5</p> <p>Ladder,8,5</p> <p>Ladder,9,5</p> <p>Ladder,9,6</p> <p>Ladder,9,7</p> <p>Ladder,9,8</p> <p>Ladder,9,9</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload