title: Convert Multidimensional Java Array to String-Matrix?
body: <p>I can't seem to get this toString() method to work? The deepToString method works perfectly, except that I have to print them out in an organized way, like a matrix would look like with aligned rows and columns. I had it working a while ago, but I changed something and now god knows what I did, I have no idea how to get it back. Anyhow, does anyone know how to output a multidimensional array to matrix like string form?</p>
  
  <p>Also, another issue I am having is figuring out how to check if the numbers >= 0, because they can't be negative. Not sure how to do that? Thought I could store each value as it loops and check if it's negative, but I just keep getting confused and/or running into errors. Any help regarding these issues would be greatly appreciated, I have been working on this for like 5hrs and have not gotten anywhere! <em>_</em></p>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>import java.util.Arrays;
  
  public class MatrixOperations {
  public static void main(String[] args) {
      double[][] matrix1 = { { 0.0, 1.0, 2.0 }, { 3.0, 4.0, 5.0 },
              { 6.0, 7.0, 0.8 }, };
  
      double[][] matrix2 = { { 1.0, 1.0, 1.0 }, { 0.0, 0.0, 0.0 },
              { 2.0, 2.0, 2.0 } };
  
      System.out.println(toString(matrix1));
      System.out.println(Arrays.deepToString(add(matrix1, matrix2)));
  }
  
  // Throws an IllegalArgumentException unless A and B contain n arrays of
  // doubles, each of
  // which contains m doubles, where both n and m are positive. (In other
  // words, both A
  // and B are n-by-m arrays.)
  //
  // Otherwise, returns the n-by-m array that represents the matrix sum of A
  // and B.
  
  public static double[][] add(double[][] A, double[][] B) {
      if (A.length != B.length || A[1].length != B[1].length) {
          throw new IllegalArgumentException("Rows and Columns Must Be Equal");
      }
      double[][] S = new double[A.length][A[1].length];
      for (int i = 0; i &lt; A.length; i++) {
          // double valueAt = ;
          for (int j = 0; j &lt; A[1].length; j++) {
              S[i][j] = A[i][j] + B[i][j];
          }
      }
      return S;
  }
  
  // Throws an IllegalArgumentException unless A contains n arrays of doubles,
  // each of
  // which contains k doubles, and B contains k arrays of doubles, each of
  // which contains
  // m doubles, where n, k, and m are all positive. (In other words, A is an
  // n-by-k array and B is a k-by-m array.)
  
  // Otherwise, returns the n-by-m array that represents the matrix product of
  // A and B.
  
  // public static double[][] mul (double[][] A, double[][] B) {
  // if (A[1].length != B.length){
  // throw new IllegalArgumentException("Column-A Must Equal Row-B");
  // }
  // }
  
  // Throws an IllegalArgumentException unless M contains n arrays of doubles,
  // each of
  // which contains m doubles, where both n and m are positive. (In other
  // words, M
  // is a n-by-m array.
  
  // Otherwise, returns a String which, when printed, will be M displayed as a
  // nicely
  // formatted n-by-m table of doubles.
  
  public static String toString(double[][] M) {
      String separator = ", ";
      StringBuffer result = new StringBuffer();
      if (M.length &gt; 0) {
          result.append(M[0]);
          for (int i = 0; i &lt; M.length; i++) {
              result.append(separator);
              result.append(M[i]);
          }
      }
      return result.toString();
  }
  }
  </code></pre>
  
  <p>Thank you for all your help! :)</p>