title: java.lang.RuntimeException: Cannot open files
body: <p>I have <code>DataManager.java</code> and <code>ExportBasicTest.java</code> for tests.</p>
  
  <p>I can't get my <code>DataManager.java</code> to pass the tests, even though they seem like basic errors.</p>
  
  <p>I tried messing around with the directories of the two files and the file that I create called <code>ExportedFile.txt</code> but I can't fix any of my errors.</p>
  
  <p>I have two <code>arrayLists</code> of type <code>&lt;Passenger&gt;</code> and <code>&lt;Flight&gt;</code> which have different parts of each. These parts are what my long methods are referencing.</p>
  
  <p><code>DataManager.java</code>:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class DataManager {
  
      private static File fileName;
      private static Formatter fileFormatter;
      private static ArrayList&lt;Flight&gt; theFlights;
      private static ArrayList&lt;Passenger&gt; thePassengers;
  
      public static void exportData(String filename, ArrayList&lt;Passenger&gt; passengers, ArrayList&lt;Flight&gt; flights) {
          fileName = new File(filename);
          theFlights = flights;
          thePassengers = passengers;
          openFile();
          addFlights();
          addPassengers();
          closeFile();
      }
  
      public static void openFile(){
          try{
              fileFormatter = new Formatter(fileName);
              System.out.println("you created a file");
          }
          catch(Exception e){
              System.out.println("You have an error.");
          }
      }
  
      public static int AlertsCount(int i){
          return thePassengers.get(i).getAlerts().size();
      }//AlertsCount
  
      public static String listAlerts(int i){
          StringBuilder stringBuilder = new StringBuilder();      
          for(int z = 0;z&lt;thePassengers.get(i).getAlerts().size();z++){
              stringBuilder.append(thePassengers.get(i).getAlerts().get(z) + System.getProperty("line.separator"));
          }
          String alerts = stringBuilder.toString();
          return alerts;
      }//listAlerts close
  
      public static int BookedFlightsCount(int i){
          return thePassengers.get(i).getBookedFlights().size();
      }//bookedFlightsCount close
  
      public static String listBookedFlights(int i){
          StringBuilder stringBuilder = new StringBuilder();      
          for(int z = 0;z&lt;thePassengers.get(i).getBookedFlights().size();z++){
              stringBuilder.append(thePassengers.get(i).getBookedFlights().get(z).getSourceAirport() + " , " + 
                      thePassengers.get(i).getBookedFlights().get(z).getDestinationAirport() + " , " +
                      Integer.toString(thePassengers.get(i).getBookedFlights().get(z).getTakeOffTime()) + " , " +
                      Integer.toString(thePassengers.get(i).getBookedFlights().get(z).getLandingTime()) + System.getProperty("line.separator"));
          }
          String bookedFlights = stringBuilder.toString();
          return bookedFlights;
      }//listBookedFlights close
  
      public static int StandbyFlightsCount(int i){
          return thePassengers.get(i).getStandbyFlights().size();
      }//StandbyFlightsCount close
  
      public static String listStandbyFlights(int i){
          StringBuilder stringBuilder = new StringBuilder();      
          for(int z = 0;z&lt;thePassengers.get(i).getStandbyFlights().size();z++){
              stringBuilder.append(System.getProperty("line.separator") + thePassengers.get(i).getStandbyFlights().get(z).getSourceAirport() + " , " + 
                      thePassengers.get(i).getStandbyFlights().get(z).getDestinationAirport() + " , " +
                      Integer.toString(thePassengers.get(i).getStandbyFlights().get(z).getTakeOffTime()) + " , " +
                      Integer.toString(thePassengers.get(i).getStandbyFlights().get(z).getLandingTime()));
          }
          String standbyFlights = stringBuilder.toString();
          return standbyFlights;
      }//listStandbyFlights close
  
      public static void addPassengers(){
              fileFormatter.format("%s%d", "#passCount ", thePassengers.size());
  
          for(int i = 0; i&lt;thePassengers.size();i++){
          fileFormatter.format("%s%s%s%s%s%s%d%s%s%d%s%s%d%s",System.getProperty("line.separator"), "#newPass",System.getProperty("line.separator"), thePassengers.get(i).getFirstName()+" , ", thePassengers.get(i).getLastName(),System.getProperty("line.separator"),
                  AlertsCount(i),System.getProperty("line.separator"), listAlerts(i), BookedFlightsCount(i),System.getProperty("line.separator"), listBookedFlights(i), StandbyFlightsCount(i), listStandbyFlights(i));
          }
      }//addPassengers close
  
      public static void addFlights(){
              fileFormatter.format("%s%d%s", "#flightCount ", theFlights.size(), System.getProperty("line.separator"));
          for(int i = 0;i&lt;theFlights.size();i++){
              fileFormatter.format("%s%s%s , %s , %d , %d%s%d%s", "#newFlight",System.getProperty("line.separator"), theFlights.get(i).getSourceAirport(), theFlights.get(i).getDestinationAirport(), 
                      theFlights.get(i).getTakeOffTime(), theFlights.get(i).getLandingTime(), System.getProperty("line.separator"),theFlights.get(i).getCapacity(),System.getProperty("line.separator"));
          }
      }//addFlights close
  
      public static void closeFile(){
          fileFormatter.close();
      }//closeFile close
  
  
      //This function creates and writes data for a new file using the specifications ,
      //given earlier in order to store the data represented by those objects stored within the two ArrayLists.
  }
  </code></pre>
  
  <p><code>ExportBasicTest.java</code>:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Scanner;
  
  import junit.framework.Assert;
  
  import org.junit.Test;
  
  
  public class ExportBasicTest {
  
      @Test(timeout = 1000)
      public void exportExampleFile() 
      {
          ArrayList&lt;Flight&gt; flightList = new ArrayList&lt;Flight&gt;();
  
          Flight f1 = new Flight("MCO", "MIA", 800, 930, 8);
          Flight f2 = new Flight("MIA", "ATL", 1100, 1400, 23);
          Flight f3 = new Flight("ATL", "GNV", 1430, 1615, 15);
  
          flightList.add(f1);
          flightList.add(f2);
          flightList.add(f3);
  
          ArrayList&lt;Passenger&gt; passList = new ArrayList&lt;Passenger&gt;();
  
          Passenger p1 = new Passenger("Joshua", "Horton");
          Passenger p2 = new Passenger("Adam", "Smith");
  
          passList.add(p1);
          passList.add(p2);
  
          p1.addAlert("The 7:30 flight from BTR to GNV has been cancelled!");
  
          p1.bookFlight(f1);
          p1.bookFlight(f2);
  
          p2.bookFlight(f3);
          p2.addStandbyFlight(f1);
          p2.addStandbyFlight(f2);
  
          DataManager.exportData("ExportedFile.txt", passList, flightList);
  
          // This file, given the initial setup, should almost perfectly match the provided example file.
          Assert.assertEquals("File contents are not as expected!", true, matchFiles("ProjStage3BasicFile.txt", "ExportedFile.txt"));
      }
  
      // Checks if the files match by directly comparing their exact contents.
      private boolean matchFiles(String expected, String actual)
      {
          Scanner inFile1;
          Scanner inFile2;
  
          try
          {
              inFile1 = new Scanner(new File(expected));
              inFile2 = new Scanner(new File(actual));
          }
          catch(IOException e)
          {
              throw new RuntimeException("Cannot open files!", e);
          }
  
          ArrayList&lt;String&gt; expectedLines = new ArrayList&lt;String&gt;();
          ArrayList&lt;String&gt; actualLines = new ArrayList&lt;String&gt;();
  
          while(inFile1.hasNextLine())
          {
              expectedLines.add(inFile1.nextLine());
          }
  
          while(inFile2.hasNextLine())
          {
              actualLines.add(inFile2.nextLine());
          }
  
          // Erase a trailing blank line at the file's end; I don't mind that.
          if(actualLines.get(actualLines.size() - 1).trim().equals(""))
          {
              actualLines.remove(actualLines.size() - 1);
          }
  
          return expectedLines.equals(actualLines);
      }
  
  }
  </code></pre>