java - Getting the opposite numbers or getting IndexOutOfBoundException -
when string layout
following:
string layout = "b....\n"+ ".....\n"+ "....b\n"+ "";
then code reads number of rows , columns wrong. row number shows me column number , vice versa. above example, number of rows 3 , columns 5. im getting indexoutofboundsexception when test examples and/or opposite numbers (instead of rows number shows columns number, , instead of columns shows rows number). smb take @ logic of code?
public static shape makeshape(string layout,char displaychar) { shape passlayout; int rows = 0; int cols = 0; string line = ""; string firstline = ""; string lastline = ""; scanner data = new scanner(layout); firstline = data.nextline(); while(data.hasnextline()) { line = data.nextline(); cols = line.length(); rows++; } //line = data.nextline(); lastline = line; if((firstline.charat(0) == '.' && lastline.charat(0) == '.') || (firstline.charat(cols) == '.' && lastline.charat(cols) == '.')) throw new fititexception("oops! empty borders!"); else passlayout = new createshape(rows, cols, displaychar, layout); return passlayout;
note: conditional if-statement checks empty chars ('.'
), i.e. layout strings shapes must have @ least 1 filled block in each of 0th row
, 0th column
, last row
, , last column
.
i'm not seeing code mixing values rows , columns, see cause of indexoutofboundsexception. try changing if statement:
if((firstline.charat(0) == '.' && lastline.charat(0) == '.') || (firstline.charat(cols-1) == '.' && lastline.charat(cols-1) == '.')) throw new fititexception("oops! empty borders!");
the position of last character 1 less number of columns because of 0 based index.
Comments
Post a Comment