Understanding the DataReader Nested Class

The DataReader class reads data from the input file and creates a SimData object for each set of wind data.

Each new SimData object is added to an ArrayList collection.
Description Code
  • In the first lines of the class a member variable is declared. The SimData objects populate the variable.
  • No constructor has been defined, so the system supplies a default constructor when a DataReader object is created.
public class DataReader {

  private List<SimData> m_flows =
    new ArrayList<SimData>();
  • Specify a method to carry out the tasks. The path to the input file is passed as an argument to the method. A try-catch construct is used to handle any exceptions when the file cannot be found. If an exception is thrown, the class opens a MessageDialog showing the error string.
public void readInput(String fileToRead) {
 
  try {
 
    FileReader fr =
      new FileReader(fileToRead);
    BufferedReader br =
      new BufferedReader(fr);
    Scanner sc =
      new Scanner(br);
 

  } catch (Exception e) {
 
    JOptionPane.showMessageDialog(
      null, e.toString()
    );
  }
}

The JOptionPane statement is included to aid in debugging; as described in Testing and Debugging.

Description Code
  • Look at the while loop which slots into the try-catch brackets. The Scanner class allows you to read each value in the file as it is encountered, as well as read the file line-by-line. By default the scanner recognizes white spaces, such as tabs, spaces, and carriage returns, as breaks between values. You can use this functionality to read each value in a line, and pass it to a SimData object. Once a SimData object has been created and stored in the collection, repeat the process for the next line in the file. A loop repeats the process "while the file has another line". Also use an additional check in the form of an if statement. A line is only read out if it contains a number, and not if it.
while (sc.hasNextLine()) {
  sc.nextLine();
  if (sc.hasNextDouble()) {
    double angDeg =
      sc.nextDouble();

    double initVelWnd =
      sc.nextDouble();
 
    SimData sd =
      new SimData(
        angDeg, , initVelWnd
      );
 
    m_flows.add(sd);
  }
}
  • Finally, a getter method is provided to allow access to the collection of SimData objects from outside the DataReaderclass.
public List<SimData> getFlowDetails() {
  return m_flows;
}

Proceed to the next section to examine the DataWriter class.