title: java two dimensional array sorting
body: <p>Write a program that prompts the user to enter a nxn matrix of double values and displays a new matrix which has the columns of the initial matrix sorted. You may use any sorting algorithm to solve the problem; please specify the name of the used sorting algorithm into your code header. Your program must implement a sorting algorithm; you cannot use the sorting methods provided in the Array class. The sorting should be implemented into a method, in which a new array is returned and the original array is intact:</p>
  
  <pre><code>public static double[][] sortCol(double[][] a)
  </code></pre>
  
  <p>The program should also implement a method that prints the initial and the result matrices to user. The print out should be nicely formatted. Here is a sample run:</p>
  
  <pre class="lang-none prettyprint-override"><code>What is the dimension of matrix? 3 
  Enter a 3x3 matrix row by row: 
  
  0.15 0.875 0.375
  
  0.55 0.005 0.225
  
  0.30 0.12 0.4
  
  The column sorted array is: 
  
  0.15 0.005 0.225
  
  0.3 0.12 0.375
  
  0.55 0.875 0.4
  </code></pre>
  
  <p>This is what I have. I believe it is almost perfect. The sorting method I used I think will sort the column but it may also be sorting the rows. However when I run the program I get this...</p>
  
  <blockquote>
    <p>Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:909)
        at java.util.Scanner.next(Scanner.java:1530)
        at java.util.Scanner.nextDouble(Scanner.java:2456)
        at Hmwk3_jrgluck.main(Hmwk3_jrgluck.java:16)</p>
  </blockquote>
  
  <p>Any ideas/ help..</p>
  
  <pre><code>import java.util.Scanner;
  
  public class sdfjasdf {
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          System.out.println("What is the dimension of your matrix?");
          int matrixdim = input.nextInt();
          double[][] matrix = new double[matrixdim][matrixdim];
  
          System.out.println("Enter " + matrixdim + " rows, and " + matrixdim
                  + " columns.");
          Scanner input1 = new Scanner(System.in);
          for (int row = 0; row &lt; matrix.length; row++) {
              for (int column = 0; column &lt; matrix.length; column++)
                  matrix[row][column] = input1.nextDouble();
          }
          System.out.println(sortCol(matrix));
      }
  
      public static double sortCol(double[][] matrix) {
          for (int i = 0; i &lt; matrix.length; i++) {
              double currentMin = matrix[i][0];
              int currentMinIndex = i;
  
              for (int j = i; j &lt; matrix.length; j++) {
                  if (currentMin &gt; matrix[j][0]
                          || (currentMin == matrix[j][0] &amp;&amp; matrix[currentMinIndex][1] &gt; matrix[j][1])) {
                      currentMin = matrix[j][0];
                      currentMinIndex = j;
                  }
              }
  
              if (currentMinIndex != i) {
                  double temp0 = matrix[currentMinIndex][0];
                  double temp1 = matrix[currentMinIndex][1];
                  matrix[currentMinIndex][0] = matrix[i][0];
                  matrix[currentMinIndex][1] = matrix[i][1];
                  matrix[i][0] = temp0;
                  matrix[i][1] = temp1;
              }
          }
          return sortCol(matrix);
      }
  }
  </code></pre>