java - Getting IndexOutofBoundsException -
why here getting indexoutofboundsexception 16 , eclipse pointing following code in line:
if((temp[0][0] == '.' && temp[0][height - 1] == '.') ||
if pass following string:
string layout = "..aa\n"+ "..aa\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "aaaa\n"+ "aaaa\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "...a\n"+ "";
into method, im getting error. smb please me this? im pretty sure im doing smth wrong in conditionals after loops.note:layout strings
shapes must have @ least 1 filled block in each of 0th row
, 0th column
, last row
, , last column
. thanks!
public static shape makeshape(string layout,char displaychar) { shape result; int height = 0; int width = 0; scanner data = new scanner(layout); while(data.hasnextline()) { string line = data.nextline(); width = line.length(); height++; } char[][] temp = new char[height][width]; scanner data2 = new scanner(layout); for(int = 0; < height; i++) { string line = data2.nextline(); for(int j = 0; j < width; j++) { if(line.charat(j) != '.') temp[i][j] = displaychar; if(line.charat(j) == '.') temp[i][j] = line.charat(j); } } data2.close(); if((temp[0][0] == '.' && temp[0][height - 1] == '.') || (temp[height - 1][0] == '.' && temp[height - 1][width - 1] == '.')) throw new fititexception("empty borders!"); result = new createshape(height, width, displaychar, temp); return result; }
you have temp[height - 1][width - 1]
first index height , second lookup width. if these confused , use height width, , height larger width, going blow up.
perhaps intended temp[0][width -1]
Comments
Post a Comment