      1  import java.io.File;

      2  import java.io.FileNotFoundException;

      3  import java.io.EOFException;

      4  import java.io.IOException;

      5  import java.util.Scanner;

      6  import java.util.NoSuchElementException;

      7

      8  /**

      9     This program processes a file containing a count followed by data values.

     10     If the file doesn't exist or the format is incorrect, you can specify another file.

     11  */

     12  public class DataAnalyzer

     13  {

     14     public static void main(String[] args)

     15     {

     16        Scanner in = new Scanner(System.in);

     17

     18        // Keep trying until there are no more exceptions

     19

     20        boolean done = false;

     21        while (!done)

     22        {

     23           try

     24           {

     25              System.out.print("Please enter the file name: ");

     26              String filename = in.next();

     27

     28              double[] data = readFile(filename);

     29

     30              // As an example for processing the data, we compute the sum

     31

     32              double sum = 0;

     33              for (double d : data) { sum = sum + d; }

     34              System.out.println("The sum is " + sum);

     35

     36              done = true;

     37           }

     38           catch (NoSuchElementException exception)

     39           {

     40              System.out.println("File contents invalid.");

     41           }

     42           catch (FileNotFoundException exception)

     43           {

     44              System.out.println("File not found.");

     45           }

     46           catch (IOException exception)

     47           {

     48              exception.printStackTrace();

     49           }

     50        }

     51     }

     52

     53     /**

     54        Opens a file and reads a data set.

     55        @param filename the name of the file holding the data

     56        @return the data in the file

     57     */

     58     public static double[] readFile(String filename) throws IOException

     59     {

     60        File inFile = new File(filename);

     61        Scanner in = new Scanner(inFile);

     62        try

     63        {

     64           return readData(in);

     65        }

     66        finally

     67        {

     68           in.close();

     69        }

     70     }

     71

     72     /**

     73        Reads a data set.

     74        @param in the scanner that scans the data

     75        @return the data set

     76     */

     77     public static double[] readData(Scanner in) throws IOException

     78     {

     79        int numberOfValues = in.nextInt(); // May throw NoSuchElementException

     80        double[] data = new double[numberOfValues];

     81

     82        for (int i = 0; i < numberOfValues; i++)

     83        {

     84           data[i] = in.nextDouble(); // May throw NoSuchElementException

     85        }

     86

     87        if (in.hasNext())

     88        {

     89           throw new IOException("End of file expected");

     90        }

     91        return data;

     92     }

     93  }