title: Java-Read Text Files And Store Them As Arrays
body: <p>I have the following piece of code. I do not understand why its not working.</p>
  
  <p>I'd really appreciate help on this.</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  class ReadFiles {
      String [] codes = new String[99];
      int i = 0;
  
      private Scanner readCodes;
  
      public void openCodesFile() {
          try {
              readCodes = new Scanner(new File("C:/Users/Carlo/Desktop/Files/codes.txt"));
          } catch (Exception e) {
              System.out.println("Could not locate the data file!");
          }
      }
  
      public void readCodesFile() {
  
          while(readCodes.hasNext()) {
              codes[i] = readCodes.nextLine();
              i++;
              System.out.println(codes[i]);
          }
      }
  
      public void closeCodesFile() {
          readCodes.close();
      }
  }
  
  class NewHardware {
      public static void main(String[] args) {
          ReadFiles codesRead = new ReadFiles();
          codesRead.openCodesFile();
          codesRead.readCodesFile();
          codesRead.closeCodesFile();
      }
  }
  </code></pre>
  
  <p>The output prints out "null" a bunch of times.</p>
  
  <p>Also, I want to be able to not only print out the codes but use the codes array in the class NewHardware and manipulate it (print it out, truncate it, etc).</p>
  
  <p>I was thinking of doing the following with readCodesFile():</p>
  
  <pre><code>public String readCodesFile() {
  
          while(readCodes.hasNext()) {
              codes[i] = readCodes.nextLine();
              i++;
              System.out.println(codes[i]);
          }
                      return (codes[i]);
      }
  </code></pre>
  
  <p>Or something but it hasn't worked just yet. Am I on the right track?</p>
  
  <p>Oh, just wanted to add that the text contains the following:</p>
  
  <pre><code>G22
  K13
  S21
  I30
  H15
  N23
  L33
  E19
  U49
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Thanks to Tony and Churk below to help me with my idiocy. I am accepting Tony's answer basically because he challenged me to think but Churk's answer is just as valuable.</p>
  
  <p>For the second part of my question (where I asked about being able to use it in class NewHardware), I did the following:</p>
  
  <pre><code>class NewHardware {
      public static void main(String[] args) {
          ReadFiles codesRead = new ReadFiles();
  
          codesRead.openCodesFile();
  
          codesRead.readCodesFile();
  
          for (int i = 0; i &lt; 9; i++) {
              System.out.println("\n\n" + codesRead.codes[i]);
          }
  
          codesRead.closeCodesFile();
      }
  }
  </code></pre>
  
  <p>This is of course not the final program code but this has helped me get the basic idea. Hope this helps others too.</p>