title: Reading File into Array - Java
body: <p>I am practicing java, and looking at exercises online:</p>
  
  <p>However, I am stuck at the point in which I need to </p>
  
  <pre><code>Read the file again, and initialise the elements of the array
  </code></pre>
  
  <p><strong>Task</strong></p>
  
  <blockquote>
    <ul>
    <li>Write class Members representing a list of members as an array</li>
    <li>Constructor should take String argument (file name)</li>
    <li>Use scanner to read lines and create array big enough to hold the file</li>
    <li>Read the file again and initialise elements of the array</li>
    </ul>
  </blockquote>
  
  <p><strong>Current Code</strong></p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  class Members {
  
      MemberElement[] members;
  
      public Members(String fileName) throws IOException {
          File myFile = new File(fileName);
          Scanner scan = new Scanner(myFile);
  
          int numOfLines = 0;
          while(scan.hasNextLine()) {
              scan.nextLine();
              numOfLines++;
          }
          scan.close();
          scan = new Scanner(myFile);
  
          members = new MemberElement[numOfLines];   
  }
  </code></pre>
  
  <p><strong>MemberElement Class</strong>:</p>
  
  <pre><code>class MemberElement {
  
      private String name;
      private int number;
      private int birthDate;
  
      public MemberElement(String name, int number, int birthDate) {
          this.name = name;
          this.number = number;
          this.birthDate = birthDate;
      }
  
      public String getName() {
          return this.name;
      }
  
      public int getNumber() {
          return this.number;
      }
  
      public int getBirth() {
          return this.birthDate;
      }
  
      public String toString() {
          return getName() + " " + getNumber() + " " + getBirth(); 
      }
  }
  </code></pre>
  
  <p>Contents Of Text File:</p>
  
  <pre><code>Wendy Miller 7654 17-2-1960
  Dolly Sheep 4129 15-5-1954
  Dolly Sheep 5132 21-12-1981
  Irma Retired Programmer 345 15-11-1946
  </code></pre>