printing array out of bounds java -
i have array of objects looking this:
1: [volvo, 200] , [jaguar, 900]
2: [bmw, 300]
3: [skoda, 100] , [(no input)] , [(no input)]
this method print areas value (with formating inside not issue). out of bound error... need do? thank you!
public static void printmat(car[][] carmat){ int row = 0; int column = 0; while ((carmat[row][column] != null)){ system.out.printf("( %-8s : %,9d )", carmat[row][column].getname(), carmat[row][column].getprice()); if (column == carmat[row].length - 1){ system.out.print("\n"); row++; column = 0; } else { column++; } } }
you have checked if going off end of current row, good, haven't checked if have run out of rows.
after row++
, add in check see if row
has gone off end of carmat
array.
if (row >= carmat.length) break;
that assumes have @ least 1 row. may want check if have @ least 1 row before enter while
loop.
Comments
Post a Comment