Skip to main content
added 2 characters in body
Source Link
Sergey Kalinichenko
  • 729k
  • 85
  • 1.2k
  • 1.6k

You are dealing with two issues here:

  • Collecting the output into a String - this can be done with StringBuilder class, 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();

You are dealing with two issues here:

  • Collecting the output into a String - this can be done with StringBuilder class, 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].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();

You are dealing with two issues here:

  • Collecting the output into a String - this can be done with StringBuilder class, 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[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();
Not appending the trailing newline.
Source Link
Sergey Kalinichenko
  • 729k
  • 85
  • 1.2k
  • 1.6k

You are dealing with two issues here:

  • Collecting the output into a String - this can be done with StringBuilder class, 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].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();

You are dealing with two issues here:

  • Collecting the output into a String - this can be done with StringBuilder class, 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].length ; col++) {
        res.append('[');
        res.append(grid[row][col]);
        res.append(']');
    }
    res.append(newline);
}
return res.toString();

You are dealing with two issues here:

  • Collecting the output into a String - this can be done with StringBuilder class, 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].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();
Source Link
Sergey Kalinichenko
  • 729k
  • 85
  • 1.2k
  • 1.6k

You are dealing with two issues here:

  • Collecting the output into a String - this can be done with StringBuilder class, 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].length ; col++) {
        res.append('[');
        res.append(grid[row][col]);
        res.append(']');
    }
    res.append(newline);
}
return res.toString();