title: JAVA Filling a 2D array from a file with an unknown amount of rows
body: <p>I am trying to figure out how to make a program that reads data from a text file, and fills a <code>Jtable</code> with it, I will need to be able to search the table, and do some calculations with the numbers.</p>
  
  <p>A row in the text file would contain:</p>
  
  <pre><code>name, country, gender, age, weight
  </code></pre>
  
  <p>The number of rows is unknown (I need to count the number of rows).
  This is what I tried, but it seems to crash. <strong>I need to count the # of rows, and then fill the array with the content from the rows</strong>.</p>
  
  <pre><code>package Jone;
  import java.io.*;
  import java.util.*;
  
  
  public class Jone  {
      public static void main (String [] args)throws IOException{
          int rows = 0;
  
          Scanner file = new Scanner (new File("data.txt"));
          while (file.hasNextLine()){rows++;}
          Object[][] data = new Object[rows][5];
          System.out.print(rows);
          file.nextLine();
          for(int i = 0;i&lt;rows;i++)
          {
              String str = file.nextLine();
              String[] tokens= str.split(",");
              for (int j = 0;j&lt;5;j++)
              {
                  data[i][j] = tokens[j]; 
  
                  System.out.print(data[i][j]);
                  System.out.print(" ");
              }         
          }
          file.close();
      }
  }
  </code></pre>