title: Reading from .data file into Java- how do I store the information in an array of arrays?
body: <p>I have the following Java class, which I'm using to read a .data file:</p>
  
  <pre><code>import java.io.*;
  import java.util.Scanner;
  
  public class ReadFile {
  static File file;
  String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
  String[] data = new String[100];
  String filename = "D:\\Users\\Elgan Frost\\Desktop\\careers\\Smart Stream Associate Software Engineer (Java) - Bristol\\assessment\\srcPerson.data";
  
  public ReadFile(File f) throws FileNotFoundException {
      // TODO Auto-generated constructor stub
      try{
          //File = new File("D:\\Users\\Elgan Frost\\Desktop\\careers\\Smart Stream Associate Software Engineer (Java) - Bristol\\assessment\\srcPerson.data");
          f = new File("D:\\Users\\Elgan Frost\\Desktop\\careers\\Smart Stream Associate Software Engineer (Java) - Bristol\\assessment\\src\\Person.data");
          Scanner scanner = new Scanner(f);
          while(scanner.hasNextLine()){
              System.out.println(scanner.nextLine());
          }
      scanner.close();
      }
      finally{
  
      }
  }
  
  public void read(File file2) throws IOException{
      FileReader fr = new FileReader("D:\\Users\\Elgan Frost\\Desktop\\careers\\Smart Stream Associate Software Engineer (Java) - Bristol\\assessment\\src\\Person.data");
      BufferedReader br = new BufferedReader(fr);
      String line;
  
      int i = 0;
      while((line = br.readLine())!= null){
          data[i] = line;
          System.out.println(data[i]);
          i++;
      }
      br.close();
  
      String[] dataNew = new String[i];
      System.arraycopy(data, 0, dataNew, 0, i);
      data = dataNew;
      System.out.println("Data length: " + data.length);
  }
  }
  </code></pre>
  
  <p>To give you a more complete understanding of my code, I also have the following Java class, which contains my main method:</p>
  
  <pre><code>import java.io.*;
  
  public class Person {
  
  public static void main(String[] args) throws IOException {
      // TODO Auto-generated method stub
      ReadFile rf = new ReadFile(f);
      rf.read(ReadFile.file);
  
  }
  static File f;
  public String id;
  public String lastName;
  public String firstName;
  public String street;
  public String city;
  
  public void readPerson() throws FileNotFoundException{
      //File f = new File("Smart Stream Associate Software Engineer (Java) - Bristol/assessment/src/Person.java");
      //InputStream IS = new FileInputStream(f);
  }
  
  }
  </code></pre>
  
  <p>Currently, when I run my program, the console displays two lines that say, "Person.data...." and give the filepath of the .data file that is being read, which will have come from the lines <code>System.out.println(scanner.nextLine());</code> and <code>System.out.println(data[i]);</code> in my ReadFile.java class, as well as the line "Data length: 1", which will have come from the line <code>System.out.println("Data length: " + data.length);</code> at the end of the class.</p>
  
  <p>I assume that this means that the program has read the first two lines of the .data file, but only stored the first.</p>
  
  <p>What I want to happen, is for it to read the first line, store that in a String, then divide the String up into separate Strings wherever a 'space' appears, and store each of those Strings in an element of the array of Strings that I created at the start of the class- by using the line:</p>
  
  <pre><code>String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
  </code></pre>
  
  <p>The separate Strings should be stored to each one in that order- so the first String (before the first space in the original String) would be stored in "personID", the second (after the first space, before the second) would be stored in "lastName", etc.</p>
  
  <p>What I then want to do, is read each of the next lines in the file in turn, storing the elements from each line into the next 'row' of my 'columns' String array (basically creating a table of data, using an array of arrays) whereby the first element of each array is a "personID", the second is a "lastName", etc.</p>
  
  <p>I want to do this until I reach the end of the file, and then print the contents of a couple of elements of the 'columns' array, just to show that it has worked.</p>
  
  <p>I'm not sure how I would go about this, and was just wondering if anyone could point me in the right direction?</p>