-2

So I wrote a program that is supposed to take a 2D array as an input and and calculate the sum of each row and each column. Then I need to use a toString() method to print the result. For example for the intake:

{3,6,8,9},{5,4,3,2}

The output should look like this:

3 6 8 9 | 26
5 4 3 2 | 14
8 10 11 11 | 40

The problem is that I don't have a clue how to print each row in a separate line using the toString method. Currently what I have is:

public String toString() {
    String output="";
    for (int i=0;i<resultArray.length;i++) {
        for (int j=0;j<resultArray[i].length;j++) {
            System.out.print(output+resultArray[i][j]+" ");                 
        }
    }

    return output;          
} 

Which returns the following: 3 6 8 9 5 4 3 2 (Don't worry about the summation methods, I have those ready I just need to understand how to format it before i'll use them)

Anyone has an idea how to print each row in its own line?

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
david
  • 1
  • 1
    Does this answer your question? [Adding New Line in Java String](https://stackoverflow.com/questions/25943597/adding-new-line-in-java-string) – Gautham M Feb 19 '21 at 05:42

2 Answers2

0
public String toString() {
    StringBuilder sb = new StringBuilder();
    int[] columns = new int[result.length]
    for (int[] row: resultArray) {
        int sum = 0;
        for (int j = 0; j < row.length; j++) {
            sb.append(row[j])
              .append(' ');
            sum += row[j];
            columns[j] += row[j];
        }
        sb.append("| ")
          .append(sum)
          .append('\n');
    }

    int total = 0;
    for (int col: columns) {
        sb.append(col)
          .append(' ');
        total += col;
    }
    sb.append("| ")
      .append(total)
      .append('\n');

    return sb.toString();
}
nomad
  • 847
  • 7
  • 10
0

Just add a new line after the inner loop:

for (int i=0;i<resultArray.length;i++) {
    for (int j=0;j<resultArray[i].length;j++) {
        System.out.print(output+resultArray[i][j]+" ");                 
    }
    System.out.print('\n');                 
}

EDIT : New line could be added using System.out.println() as well. Even though this works in normal scenarios, it might not work as expected in one case. As the javadoc states for println() :

Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n')".

So if the same code is run after the line.separator property is overriden, then the code will not work as expected. Hence, if a new line is to be added, it is better to use System.out.print('\n') or System.out.print("\n") instead of System.out.println().

Gautham M
  • 4,816
  • 3
  • 15
  • 37
  • Thank You! I knew it should be something simple lol – david Feb 19 '21 at 16:07
  • or just System.out.println(); – nomad Feb 19 '21 at 20:26
  • @nomad just using`System.out.println()` without passing any arguments works well in normal scenarios, but sometimes it might not work as expected. As the javadoc states - "Terminates the current line by writing the line separator string. The line separator string is defined by the system property `line.separator`, and is not necessarily a single newline character ('\n')". So if the same code is run after the `line.separator` property is overriden, then the code will not work as expected. So it is better to use `System.out.print('\n')` (or `System.out.print("\n")`) – Gautham M Feb 20 '21 at 09:36