checking if a matrix is square in Java before assigning it to a 2 D array
<p>I'm trying to assign a 2D arraylist to a 2D  int array.
And in the process I'm also trying to check if the input is a square matrix.</p>

<p>Here's my code:</p>

<pre><code>/** Basically trying to create a 2D ArrayList from the input here.**/


        input = new Scanner(new File(textFileName));
        while (input.hasNextLine()) {
            Scanner colReader = new Scanner(input.nextLine());
            ArrayList&lt;Integer&gt; col = new ArrayList&lt;Integer&gt;();
            while (colReader.hasNextInt()) {
                col.add(colReader.nextInt());
            }
            dynamicInputMatrix.add(col);
        }


//Assigning elements from the 2D ArrayList to the  conventional 2D array.

input2DArray = new int[dynamicInputMatrix.size()][dynamicInputMatrix.size()];

            int i = 0;
            int j = 0;
            for (ArrayList&lt;Integer&gt; l : dynamicInputMatrix) {
                j = 0;
                while (j &lt; l.size()) {
                    for (Integer k : l) {


                        input2DArray[i][j] = k; 
                        j++;
                    }
                    i++;
                }

if (input2DArray.length!=input2DArray[0].length) {
    throw new  IllegalStateException("The input needs to be a square matrix!"); // This code is never accessed!
}

            }
</code></pre>

<p>Sample Input : that causes it to fail..  A 7 x 6 matrix</p>

<pre><code>0 2 0 0 0 4 
1 0 0 1 0 0 
1 0 0 7 0 8 
3 0 0 0 0 0 
4 5 0 0 0 0 
0 0 0 0 0 3 
0 0 0 0 0 9
</code></pre>

<p>I expect the Illegal exception to be thrown now, but that isnt happening. Any tips on what I'm doing wrong would be helpful</p>

<p><em>A Java rookie</em></p>
