title: Arrays of Unknown Size in Java
body: <p>I am writing a program in Java to do a number of different mathematical operations and formulas and one of these is matrix addition. I needed a way to make an array of size decided by the user. I could have the user input the dimensions of the array but I wanted a more seamless approach. I decided to have the input the contents of the array by typing the contents separated by commas for different rows and by spaces for different numbers (1 2 3, 4 5 6, 7 8 9 for a 3x3 array) and this is the code i came up with. (sc is the scanner)</p>
  
  <pre><code>String[] pr = sc.nextLine().split(", ");
  for(int k = 0; k &lt; pr.length; k++){
      String[] pc = pr[k].split(" ");
      for(int j = 0; j &lt; pc.length; k++){
          pt[k][j] = pc[j];
      }
  }
  </code></pre>
  
  <p>This code does not work; I get a <code>java.lang.NullPointerException</code> if I run it. Initially I was looking for a way to make a 2 dimensional array just by reading one input, something like:</p>
  
  <pre><code>String[][] pt = sc.nextLine().split(", ")(" ");
  </code></pre>
  
  <p>where it would split with the commas for the different rows and with the spaces for columns within the rows. I don't think anything like this exists in Java, but if it does that would be great. Otherwise, I would appreciate all the help I could get with this. If there is no easy solution to this I might just have to have the user type the array dimensions.</p>
  
  <p>Edit: If this isn't possible then if anyone else has a working method for storing matrices then that would be great.</p>