title: Reading data file into multidimensional array Java
body: <p>I have a .dat file that I want to load into a custom array.  How do I get it to actually load the data into the array.  The data consists of a (String, int, int, double, String).</p>
  
  <pre><code>class CDinventoryItem{
  
  
     private CDinventoryItem [] inven = new CDinventoryItem[1000];
  
  
      public CDinventoryItem (String title, int itemNumber, int numberofUnits, 
      double unitPrice, String genre){
  
                DataInputStream input;
  
              try{
  
                  input = new DataInputStream(new FileInputStream("inventory.dat"));
  
                  inven = input.read(CDinventoryItem[]);  //line I am receiving error on
  
  
              }
              catch ( IOException error ){
                  JOptionPane.showMessageDialog( null, "File not found",
                  "" ,JOptionPane.ERROR_MESSAGE);
              }
  
  
          }
  
  }
  </code></pre>
  
  <p>So now readFile is in its own class...  </p>
  
  <pre><code>class readFile {
  public CDinventoryItem[] inven;
  
      public readFile(){
  
  BufferedReader in = null;
  
  try {
      in = new BufferedReader(new FileReader("inventory.dat"));
      String line = null;
      int i = 0;
      while ((line = in.readLine()) != null) {
  
          // process each line
          String[] parts = line.split(",");
          String title = parts[0];
          int itemNumber = Integer.parseInt(parts[1]);
          int numberofUnits = Integer.parseInt(parts[2]);
          double unitPrice = Double.parseDouble(parts[3]);
          String genre = parts[4];
  
          CDinventoryItem item = new CDinventoryItem(title, itemNumber, numberofUnits, 
  unitPrice, genre);
  
          //add item to array
          inven[i] = item;
          i++;
      }
  } catch (IOException e) {
      e.printStackTrace();
  } finally {
      if (in != null) {
          try {
              in.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  }}
  </code></pre>
  
  <p>and I am calling it from my CDinventory class</p>
  
  <pre><code>        readFile invenItem = new readFile();
          list = new JList(invenItem.inven);
  </code></pre>
  
  <p>but it gives me a: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
  on line:
      readFile invenItem = new readFile();</p>
  
  <p>Doesn't seem to like me passing the array that way.</p>