title: Transposing a matrix from a 2D array
body: <p>I'm self teaching myself some java and I'm stuck on creating a 2D array that initializes it with random values and then creates the transpose of the array.  </p>
  
  <p>An example output is:</p>
  
  <pre><code>$ java Test1 22 333 44 555 6  
  Enter the number of rows (1-10): 0  
  ERROR: number not in specified range (1-10) !  
  and so on until you enter the correct number of rows and columns.
  </code></pre>
  
  <h3>Original matrix</h3>
  
  <blockquote>
  <pre><code>  1  22  
  333  44  
  555   6
  </code></pre>
  </blockquote>
  
  <h3>Transposed matrix</h3>
  
  <blockquote>
  <pre><code>  1  333  555`  
   22   44    6`
  </code></pre>
  </blockquote>
  
  <p>^ Should be the final output.  Some help with the code would appreciated!</p>
  
  <p>I would like to code to generate error messages if the number of rows or columns is outside the specified range. And for if to read the matrix elements from the command line and not generate them randomly. </p>
  
  <pre><code>import java.util.Scanner;
  
  public class Test1 {
      /** Main method */
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          System.out.print("Enter the number of rows (1-10): ");
          int rows = input.nextInt();
          System.out.print("Enter the number of columns (1-10): ");
          int cols = input.nextInt();
  
          // Create originalMatrix as rectangular two dimensional array
          int[][] originalMatrix = new int[rows][cols];
  
          // Assign random values to originalMatrix
          for (int row = 0; row &lt; originalMatrix.length; row++)
              for (int col = 0; col &lt; originalMatrix[row].length; col++) {
                  originalMatrix[row][col] = (int) (Math.random() * 1000);
              }
  
          // Print original matrix
          System.out.println("\nOriginal matrix:");
          printMatrix(originalMatrix);
  
          // Transpose matrix
          int[][] resultMatrix = transposeMatrix(originalMatrix);
  
  
          // Print transposed matrix
          System.out.println("\nTransposed matrix:");
          printMatrix(resultMatrix);
      }
  
      /** The method for printing the contents of a matrix */
      public static void printMatrix(int[][] matrix) {
          for (int row = 0; row &lt; matrix.length; row++) {
              for (int col = 0; col &lt; matrix[row].length; col++) {
                  System.out.print(matrix[row][col] + "  ");
              }
              System.out.println();
          } 
      }
  
      /** The method for transposing a matrix */
      public static int[][] transposeMatrix(int[][] matrix) {
          // Code goes here...
      }
  }
  </code></pre>