//From an example in Big Java, by Cay Horstman import java.io.*; import java.util.*; public class DataSetReader { //Instance Variable: stores data from input file private double[] data; //"Opens" the named file to make it available for reading //Creates a Scanner to interpret the text file public double[] readFile(String fileName) throws IOException { FileReader reader = new FileReader(fileName); Scanner scan = new Scanner(reader); try { readData(scan); } finally { reader.close(); } return this.data; } //Instantiate an array to store the input data //Coordinates input and storing of the data private void readData(Scanner scan) { if ( !scan.hasNextInt() ) throw new DataException("Integer count expected"); int dataCount = scan.nextInt(); this.data = new double[dataCount]; for (int index = 0; index < dataCount; index++) readItem(scan, index); if ( scan.hasNext() ) throw new DataException("End of file expected"); } //Inputs the next data item from the file // and stores it into the data array private void readItem(Scanner scan, int index) { if ( !scan.hasNext() ) throw new DataException("Another data item expected"); if ( !scan.hasNextDouble() ) throw new DataException("Double value expected. Found > " + scan.next() + " < instead"); double item = scan.nextDouble(); data[index] = item; } }