arrays - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 -
i'm getting error:
exception in thread "main" java.lang.arrayindexoutofboundsexception: -1 @ snakebox.neighbor(snakebox.java:150) @ snakebox.findsnake(snakebox.java:86) @ pg6a.main(pg6a.java:28)
the class being used is:
/** class can used manipulate 2d boxes of "snakes" pg6. */ import java.util.*; import java.io.*; public class snakebox { // instance variables private char[][] box; private int rows, columns; private int snakecount; private int startrow, startcol; private int endrow, endcol; private boolean finish; private int x, y; private string todisplay; /** create , initialize snakebox reading file. @param filename external name of plain text file */ public snakebox(string filename) throws ioexception{ scanner filescan = new scanner(new filereader(filename)); rows = filescan.nextint(); columns = filescan.nextint(); snakecount = filescan.nextint(); filescan.nextline(); box = new char[rows][columns]; (int i=0; < rows; i++) { string line = filescan.nextline(); (int j=0; j < columns; j++) { char character = line.charat(j); box[i][j] = character; } } } /** create new snakebox of given size , snake count @param rows height of box @param cols number of columns @param snakes how many snakes in box */ public snakebox(int rows, int cols, int snakes) { this.rows = rows; this.columns = cols; this.snakecount = snakes; } /** display box on screen. */ public void display() { string todisplay = ""; (int row = 0; row < rows; row++) { (int column = 0; column < columns; column++) { todisplay += box[row][column]; } todisplay += '\n'; } system.out.print(todisplay); } /** find next snake, skinning change s . */ public void findsnake() { // check make sure there still snakes skin if (finish = true) { int row, col, nb; // find s search outerloop: (int k=0; k < rows; k++) { (int l=0; l < columns; l++) { if (box[k][l] == 's') { startrow = k; startcol = l; endrow = k; endrow = l; break outerloop; } } } // set initial s position both start , end // search end, updating go { nb = neighbor(endrow, endcol); switch (nb) { case 1: endrow--; endcol--; break; case 2: endrow--; break; case 3: endrow--; endcol++; break; case 4: endcol--; break; case 5: endcol++; break; case 6: endcol--; endrow++; break; case 7: endrow++; break; case 8: endrow++; endcol++; break; } } while (nb != 0); // search start, updating go { nb = neighbor(startrow, startcol); switch (nb) { case 1: startrow--; startcol--; break; case 2: startrow--; break; case 3: startrow--; startcol++; break; case 4: startcol--; break; case 5: startcol++; break; case 6: startcol--; startrow++; break; case 7: startrow++; break; case 8: startrow++; startcol++; break; } } while (nb != 0); // update snake count snakecount = snakecount - 1; //display start/end points of snake, display } /** change position s . , find neighboring s if 1 exists. @param x row number of position change @param y column number of position change @return 0 if no s neighbor found, or number grid indicating position of found neighbor 1 2 3 4 s 5 6 7 8 */ } private int neighbor(int x, int y) { box[x][y] = '.'; if (box[x--][y--] == 's') return 1; if (box[x--][y] == 's') return 2; if (box[x--][y++] == 's') return 3; if (box[x][y--] == 's') return 4; if (box[x][y++] == 's') return 5; if (box[x++][y--] == 's') return 6; if (box[x++][y] == 's') return 7; if (box[x++][y++] == 's') return 8; else return 0; } /** display endpoints of snake skinned. */ public void printends() { system.out.print("(" + x + "," + y + ")"); } /** find out how many full snakes in box. @return snake count */ public int getcount() { return snakecount; } /** check whether snakes have been skinned, based on snake count. @return true if done, false otherwise */ public boolean finished() { if (snakecount == 0) { finish = true; } else { finish = false; } return finish; } }
also, driver program (pg6) is:
import java.util.*; import java.io.*; public class pg6a { public static void main(string[] args) throws ioexception { scanner keyboard = new scanner(system.in); system.out.println("please enter name of file snake box resides, , try our best eliminate snakes in snake box!"); string filename; filename = keyboard.nextline(); snakebox snakes = new snakebox(filename); snakes.display(); while (snakes.finished() == false) { snakes.findsnake(); snakes.printends(); snakes.display(); system.out.print(snakes.getcount()); } } }
its because of if
statements in neighbor()
.
if (box[x--][y--] == 's')
- whether evaluates true or false
, decrement x
& y
.
similarly, if
statements, keep decrementing
or incrementing
x & y
. that's why @ point, value of either x
or y
or both go below 0
, giving index
of -1
in array. , hence arrayindexoutofboundsexception
.
you can fix using x-1
& y+1
in if
statements, instead of increment/decrement
operators.
this line snakebox.neighbor(snakebox.java:150)
says error exists in neighbor
method , looking @ if
statements in neighbor()
method, tells have possibly gone wrong.
Comments
Post a Comment