You are dealing with two issues here:
- Collecting the output into a
String- this can be done withStringBuilderclass, and - Adding a newline to the output string - this is explained in this Q&A
The end result should look as follows:
StringBuilder res = new StringBuilder();
String newline = System.getProperty("line.separator");
for (int row = 0 ; row < grid.length ; row++) {
for (int col = 0 ; col < grid[0]grid[row].length ; col++) {
res.append('[');
res.append(grid[row][col]);
res.append(']');
}
// Do not append the trailing newline
if (row != grid.length-1) {
res.append(newline);
}
}
return res.toString();