Computer Science 121
Example using a StreamTokenizer

import java.io.*;
import java.util.*;

/* This application uses an array of classes to store information read from a file. The file data is separated into tokens by a StreamTokenizer. */
public class Weather
{
    public static void main (String [] args)
    {
        WeeksWeather week = new WeeksWeather ();
        week.readTemperatures ();
        week.displayStatistics ();
    } // main method
} // class Weather

/* The WeatherData class stores three pieces of information about the weather for a single day. */
class WeatherData
{
    private String dayName;
    private int high, low;

    WeatherData ()
    {
        high = 0;
        low = 0;
        dayName = "";
    } // constructor

    public int getHigh ()
    {
        return high;
    } // method getHigh

    public int getLow ()
    {
        return low;
    } // method getLow

    public void readData (StreamTokenizer tokens) throws IOException
    {
        int next;
        dayName = tokens.sval;
        next = tokens.nextToken ();
        high = (int) tokens.nval;
        next = tokens.nextToken ();
        low = (int) tokens.nval;
    } // method readData

    public void displayTemps ()
    {
        System.out.println (dayName + " High: " + high + " Low: " + low);
    } // method displayTemps
} // class WeatherData

/* This class reads data from a file and stores it in an array of classes. It uses a StreamTokenizer to divide the data into separate tokens. */
class WeeksWeather
{
    private WeatherData [] temperatures;
    private int numberTemps;
    final int maxSize = 10;

    WeeksWeather ()
    {
        numberTemps = 0;
        temperatures = new WeatherData [maxSize];
    } // constructor

    public void readTemperatures ()
    {
        int next;
        WeatherData data;
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        try
        {
            System.out.print ("File Name: ");
            String fileName = stdin.readLine ();
            BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream (fileName)));
            StreamTokenizer tokens = new StreamTokenizer (infile);
            next = tokens.nextToken ();
            while (next != tokens.TT_EOF)
            {
                data = new WeatherData ();
                data.readData (tokens);
                data.displayTemps ();
                temperatures [numberTemps] = data;
                numberTemps ++;
                next = tokens.nextToken ();
            }
        } // try block
        catch (IOException e) { System.out.println ("IO Exception");}
        catch (NumberFormatException ex) { System.out.println ("Number format error.");}
    } // method readTemperatures

    private int calculateAverageHigh ()
    {
        int sum = 0;
        for (int count = 0; count < numberTemps; count ++)
            sum = sum + temperatures [count].getHigh ();
        int average = sum / numberTemps;
        return average;
    } // method calculateAverage

    private int findHighest ()
    {
        int highest;
        highest = temperatures [0].getHigh ();
        for (int count = 1; count < numberTemps; count ++)
        {
            if (highest < temperatures [count].getHigh ())
                highest = temperatures [count].getHigh ();
        }
        return highest;
    } // method findHighest

    private int findLowest ()
    {
        int lowest;
        lowest = temperatures [0].getLow ();
        for (int count = 1; count < numberTemps; count ++)
         {
            if (lowest > temperatures [count].getLow ())
                lowest = temperatures [count].getLow ();
         }
        return lowest;
    } // method findLowest

    public void displayStatistics ()
    {
        int average = calculateAverageHigh ();
        int highest = findHighest ();
        int lowest = findLowest ();
        System.out.println ("The average high was " + average);
        System.out.println ("The highest temperature was " + highest);
        System.out.println ("The lowest temperature was " + lowest);
    } // method displayAverage
} // class WeeksWeather

/* Program output:
Sunday High: 71 Low: 62
Monday High: 59 Low: 45
Tuesday High: 66 Low: 49
Wednesday High: 58 Low: 42
Thursday High: 52 Low: 44
Friday High: 63 Low: 52
Saturday High: 73 Low: 59
The average high was 63
The highest temperature was 73
The lowest temperature was 42
*/