title: How to print a linked list into a file
body: <p>Each linkedlist has a ISBN, authors name, book date, book price and I'm trying to save the whole book catalog (x many books) as a file. They said there is an error at </p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 21
  at BookCatalog.SaveFile(BookCatalog.java:34)
  at BookCatalogClient.menu(BookCatalogClient.java:167)
  at BookCatalogClient.main(BookCatalogClient.java:25)
  </code></pre>
  
  <p>which java:167 = for(Book newBook: g.SaveFile())</p>
  
  <p>Here is the code:</p>
  
  <p>from class 1:</p>
  
  <pre><code>PrintStream out = new PrintStream(new FileOutputStream("books.txt"));
  for(Book newBook: g.SaveFile()){
  out.println(newBook.getBookISBN()+"\t"+newBook.getLastName()+"\t"
  +newBook.getFirstName()+"\t"+newBook.getTitle()+"\t"+newBook.getYearOfPublication()+"\t"+newBook.getPrice());
  
  }
  </code></pre>
  
  <p>from class 2:</p>
  
  <p>count is a global var that keeps how many books are in the catalog.</p>
  
  <pre><code>public Book[] SaveFile(){
  
     Book cursor = head;
     Book[] bookCount = new Book[count-1];
     int i = 0;
     while(cursor!=null){
         Book out = cursor;
         cursor = cursor.getNext();
         bookCount[i] = out;
         i++;
  
     }
     return bookCount;
  </code></pre>
  
  <p>}</p>