import java.io.File ; import java.io.IOException; import java.util.Scanner; /** Reads a data set from a file. The first line of the file must contain an int, which is the number of additional lines in the file. Each additional line must contain a double value. */ public class DataSetReader { private double[] data ; // array instance variable /** Reads a data set. @param filename the name of the file holding the data @return the data in the file */ public double[] readFile(String filename) throws IOException, BadDataException { Scanner in = null ; try { in = new Scanner(new File(filename)) ; readData(in); } finally { if (in != null) // if file was opened... in.close(); // ...close it } return data; } /** Reads all data. @param in the scanner that scans the data */ private void readData(Scanner in) throws BadDataException { if (!in.hasNextInt()) // first record does not contain an int throw new BadDataException("First record must be a valid int." + " Found: " + in.next()); int numberOfValues = in.nextInt() ; // get number of values in file data = new double[numberOfValues] ; // create array with that number // of elements // extract numberOfValues values from file and store in array "data" // in the array for (int i = 0; i < numberOfValues; i++) { if (!in.hasNext()) // no more tokens, reached eof premturely throw new BadDataException ("Unexpected end-of-file when atempting to read value #" + (i+1) + " of " + numberOfValues) ; if (!in.hasNextDouble()) // next token not a valid double throw new BadDataException("Need valid double! Found: " + in.next() + " (in record #" + (i+2) +")"); // extract next double and store at specified index data[i] = in.nextDouble(); } // here only if numberOfValues doubles have been succesfully read // is there anything left in the file after extracting the specified // number of values? if (in.hasNext()) throw new BadDataException("End of file expected"); } }