title: Java File I/O validate file contents then use those contents to create sales report
body: <p>I'm in a <strong>beginner</strong> Java course...and my latest assignment seems super difficult for some reason.  The assignment is focused around File I/O.  It involves many parts, but this part is what is really confusing me. It involves:</p>
  
  <ol>
  <li>Reading a Sales file where each line is supposed to have first the
  Division (N, S, E or W), then the Quarter (1-4) on the next line,
  then the Sales Amount on the third line and then repeats. <strong>(can do
  this ok)</strong></li>
  <li>We have to validate that each line has one of the allowable
  options/file types <strong>(can do this ok)</strong></li>
  <li>Then dump any errors to an error file and ask the user to supply
  another file &amp; repeat until the input file is in the right format.
  <strong>(have this working)</strong></li>
  <li>Once it is in the right format, we have to be able to generate a
  sales report in the below format <strong>(can't figure out how I am
  supposed to be able to get this collected...)</strong></li>
  </ol>
  
  <blockquote>
    <p>Sales Report </p>
    
    <p>Total Sales by division:<br>
    North $total<br>
    South $total<br>
    East $total<br>
    West $total  </p>
    
    <p>Total Sales by quarter:<br>
    Jan-Mar $total<br>
    Apr-Jun $total<br>
    Jul-Sep $total<br>
    Oct-Dec $total  </p>
    
    <p>Year End Total: $total  </p>
  </blockquote>
  
  <p>One menu option has to be to Validate the file is in the correct format, and then another menu option appears to generate the Sales Report.</p>
  
  <p><strong>Note: We aren't expected to use arrays yet (that's the next assignment).</strong></p>
  
  <pre><code> import java.io.*;
   import java.util.Scanner;
  
   public class SalesReporting {
      public static void main(String[] args) throws Exception {
      String inputData;
      String divisionRead;
      int qtrRead;
      double salesRead;
      String filePath;
      String menuChoice;
      File fileName;
      PrintWriter outputErrorLog;
      PrintWriter outputSalesReport;
      Scanner keyboard = new Scanner(System.in);
      Scanner inputFile;
      String overwriteFile;
      Boolean isDataFileError;
      Boolean isValidSalesFile = false;
      Boolean isValidMenuChoice;
      Boolean isToOverwrite = false;
  
      System.out.println("\tHarmway Corporation" +
              "\nQuarterly Sales Report Generator");
  
      do { 
          do {
              isValidMenuChoice = true;
  
              // display main menu options to user &amp; prompt for choice
              System.out.print("\n\tMain Menu" +
                  "\n" +
                  "\n[V]alidate Sales Data");
              // only display generate sales report option if a sales report has been validated
              if (isValidSalesFile) {
                  System.out.print("\n[G]enerate Sales Report");
              }
              System.out.print("\nE[x]it program" +
                  "\n");
  
              System.out.print("\nEnter choice: ");
  
              menuChoice = keyboard.next();
  
              if (!menuChoice.equalsIgnoreCase("v") &amp;&amp;
                  !menuChoice.equalsIgnoreCase("g") &amp;&amp;
                  !menuChoice.equalsIgnoreCase("x")) {
                  isValidMenuChoice = false;
                  System.out.print("\n\t**Error** - Invalid menu item");
              }
          } while (isValidMenuChoice == false);
  
          if (menuChoice.equalsIgnoreCase("v")) {
              do {
                  // prompt user for the sales data file path
                  System.out.print("\nSales data file path: ");
                  filePath = keyboard.next();
  
                  fileName = new File(filePath);
  
                  // if the file path doesn't exist, error displayed
                  if (!fileName.exists()) {
                      System.out.println("\n" + fileName + " not found");
                  }
              } while (!fileName.exists());
  
              // create a scanner for the input file
              inputFile = new Scanner(fileName);
  
              // create an error log to dump invalid sales data errors to
              fileName = new File("errorlog.txt");
              outputErrorLog = new PrintWriter(fileName);
  
              // resets boolean to allow for error free data file check
              isDataFileError = false;
  
              // validate and store the sales data
              while (inputFile.hasNext())
              {
                  // first line must be division N, S, E or W to be valid
                  inputData = inputFile.next();
                  try 
                  {
                      divisionRead = inputData;
  
                      if (isValidDivision(divisionRead)) {
                          System.out.println("ok division");
                      }
  
                      else 
                      {
                          isDataFileError = true;
                          outputErrorLog.println(divisionRead + ",Invalid division value");
                      }
                  } 
                  catch (Exception e) 
                  {
                      isDataFileError = true;
                      outputErrorLog.println(inputData + ",Invalid division value");
                  }
  
                  // if second line is a valid quarter
                  inputData = inputFile.next();
                  try 
                  {
                      qtrRead = Integer.parseInt(inputData);
  
                      if (isValidQuarter(qtrRead)) {
                          System.out.println("ok quarter");
                      }
  
                      else
                      {
                          isDataFileError = true;
                          outputErrorLog.println(qtrRead + ",Invalid quarter value");
                      }
                  } 
                  catch (Exception e) 
                  {
                      isDataFileError = true;
                      outputErrorLog.println(inputData + ",Invalid quarter value");
                  }
  
                  inputData = inputFile.next();
                  try 
                  {
                      salesRead = Double.parseDouble(inputData);  
  
                      if (isValidSales(salesRead)) {
                          System.out.println("ok sales");
                      }
  
                      else
                      {
                          isDataFileError = true;
                          outputErrorLog.println(salesRead + ",Invalid sales amount value");
                      }
                  } 
                  catch (Exception e) 
                  {
                      isDataFileError = true;
                      outputErrorLog.println(inputData + ",Invalid sales amount value");
                  }
  
  
              }
  
  
              // close the input sales data file
              inputFile.close();
  
              // close the error log file to write
              outputErrorLog.close();
  
              if (isDataFileError)
              {
                  // there was an error in the sales file; not a valid sales data file
                  isValidSalesFile = false;
                  System.out.print("\nThe data file contains data errors"
                      + " (See error log for details)");
              }
              else
              {
                  // there were no errors; valid sales data file
                  isValidSalesFile = true;
                  System.out.print("\nThe data file is validated");
              }
          }
  
          if (menuChoice.equalsIgnoreCase("g")) {
              System.out.println("\ntemp g");
  
              do {
                  // prompt user for file path to save sales report
                  System.out.print("\nSave report as: ");
                  filePath = keyboard.next();
  
                  // identify file to be used
                  fileName = new File(filePath);
  
  
                  // file already exists; ask whether to overwrite file or not
                  if (fileName.exists())
                  {
                      do 
                      {
                          System.out.print("\nOverwrite (y/n): ");
                          overwriteFile = keyboard.next();
  
                          if (overwriteFile.equalsIgnoreCase("y"))
                          {
                              isToOverwrite = true;
  
                              // create printwriter for the sales data file
                              outputSalesReport = new PrintWriter(filePath);
  
                              System.out.println("\nGenerating sales report...");
  
  
  
                              // add code to write to the results to the file
  
  
  
  
  
                              // close sales report
                              outputSalesReport.close();
  
                              System.out.println("\nReport generated successfully!"); 
                          }
  
                      } while (!overwriteFile.equalsIgnoreCase("n") &amp;&amp;
                              !overwriteFile.equalsIgnoreCase("y"));
                  }
                  // file doesn't already exist; save the file
                  else if (!fileName.exists())
                  {
                      // create printwriter for the sales data file
                      outputSalesReport = new PrintWriter(filePath);
  
                      System.out.println("\nGenerating sales report...");
  
  
  
                      // add code to write to the results to the file
  
  
  
  
  
                      // close sales report
                      outputSalesReport.close();
  
                      System.out.println("\nReport generated successfully!");
                  }   
              } while (!isToOverwrite);
  
  
          }
  
          if (menuChoice.equalsIgnoreCase("x")) {
              System.out.println("\nProgram has been terminated.");
              System.exit(0);
          }
  
      } while (!menuChoice.equalsIgnoreCase("x"));
  
  }
  
  
  
  public static boolean isValidDivision(String divisionRead) {
      if (divisionRead.equalsIgnoreCase("N") ||
              divisionRead.equalsIgnoreCase("S") ||
              divisionRead.equalsIgnoreCase("E") ||
              divisionRead.equalsIgnoreCase("W")) {
          return true;
      }
  
      else {
          return false;
      }
  }
  
  public static boolean isValidQuarter(int qtrRead) {
      if (qtrRead &gt;= 1 &amp;&amp; qtrRead &lt;= 4) {
          return true;
      }
  
      else {
          return false;
      }
  }
  
  public static boolean isValidSales(double salesRead) {
      if (salesRead &gt;= 0) {
          return true;
      }
  
      else {
          return false;
      }
  }
  }
  </code></pre>
  
  <p>I'm not asking for specific code, but just some guidance on how I should best proceed.  Any advice would be appreciated.  I've been stuck on this assignment for over a month! </p>
  
  <p>Example of what the Sales Data file would look like if in a valid format (each would be on its own line...the forum is ignoring the breaks):</p>
  
  <p>N<br>
  1<br>
  35.50<br>
  N<br>
  2<br>
  26.99<br>
  N<br>
  3<br>
  77.45<br>
  N<br>
  4<br>
  58.30<br>
  S<br>
  1<br>
  132.15<br>
  S<br>
  2<br>
  81.19<br>
  S<br>
  3<br>
  159.06<br>
  S<br>
  4<br>
  83.55<br>
  E<br>
  1<br>
  99.40<br>
  E<br>
  2<br>
  25.39<br>
  E<br>
  3<br>
  50.25<br>
  E<br>
  4<br>
  43.21<br>
  W<br>
  1<br>
  120.89<br>
  W<br>
  2<br>
  392.11<br>
  W<br>
  3<br>
  105.76<br>
  W<br>
  4<br>
  299.95<br>
  N<br>
  2<br>
  66.15<br>
  N<br>
  3<br>
  38.22<br>
  N<br>
  4<br>
  27.66<br>
  E<br>
  2<br>
  135.32<br>
  E<br>
  3<br>
  37.50<br>
  E<br>
  4<br>
  9.10  </p>