for (int j = 1; j < (length + 1); j++)

length of what?