DataAnalyzer.java.html

  1  import java.io.FileNotFoundException;
  2  import java.io.IOException;
  3  import java.util.Scanner;
  4  
  5  /**
  6     This program reads a file containing numbers and analyzes its contents.
  7     If the file doesn't exist or contains strings that are not numbers, an
  8     error message is displayed.
  9  */
 10  public class DataAnalyzer
 11  {
 12     public static void main(String[] args)
 13     {
 14        Scanner in = new Scanner(System.in);
 15        DataSetReader reader = new DataSetReader();
 16        
 17        boolean done = false;
 18        while (!done) 
 19        {
 20           try 
 21           {
 22              System.out.println("Please enter the file name: ");
 23              String filename = in.next();
 24              
 25              double[] data = reader.readFile(filename);
 26              double sum = 0;
 27              for (double d : data) { sum = sum + d; }
 28              System.out.println("The sum is " + sum);
 29              done = true;
 30           }
 31           catch (FileNotFoundException exception)
 32           {
 33              System.out.println("File not found.");
 34           }
 35           catch (BadDataException exception)
 36           {
 37              System.out.println("Bad data: " + exception.getMessage());
 38           }
 39           catch (IOException exception)
 40           {
 41              exception.printStackTrace();
 42           }
 43        }
 44     }
 45  }