Computer Science Example
Using a StreamTokenizer to read data from a file.

import java.io.*;
 
/* This application reads data in from a file and stores it into an array of classes.  It then processes the data. */
public class Weather
{
     public static void main (String [] args)
     {
          CitiesWeather city = new CitiesWeather ();
          city.readTemperatures ();
          city.displayStatistics ();
     } // main method
} // class Weather

// class that stores information about the weather in a city.
class WeatherData
{
     private String cityName;
     private int high, low, average;
 
     // Accessor methods for high and average.
     public int getHigh () {return high;}
     public int getAverage () {return average;}
 
     // Method that uses a StreamTokenizer to read data from a file.
     public void readCityData (StreamTokenizer tokens) throws IOException
     {
          int next;
          cityName = tokens.sval;
          next = tokens.nextToken ();
          high = (int) tokens.nval;
          next = tokens.nextToken ();
          low = (int) tokens.nval;
          average = (high + low) / 2;
     } // method readData
 
     // Method that echoes on the screen the data read in.
     public void echoData ()
     {
          System.out.println (cityName + " High: " + high + " Low: " + low);
     } // method echoData
} // class WeatherData

/* This class reads high and low temperatures into an array of classes using a StreamTokenizer.  It then calculates the average temperature and displays the result on the screen. */
class CitiesWeather
{
     final int maxSize = 10;
     WeatherData [] temperatures = new WeatherData [maxSize];
     int numberCities = 0;
 
     // Method that uses a StreamTokenizer to read data from a file into an array of classes.
    public void readTemperatures ()
    {
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("citiesWeather.txt")));
               StreamTokenizer tokens = new StreamTokenizer (infile);
               int next = tokens.nextToken ();
               while (next != tokens.TT_EOF)
               {
                    WeatherData data = new WeatherData ();
                    data.readCityData (tokens);
                    data.echoData ();
                    temperatures [numberCities] = data;
                    numberCities ++;
                    next = tokens.nextToken ();
               }
               infile.close ();
          } // try block
          catch (ArrayIndexOutOfBoundsException e) {System.out.println ("File too large");}
          catch (IOException e) { System.out.println ("IO Exception");}
          catch (NumberFormatException ex) { System.out.println ("Number format error.");}
     } // method readTemperatures

     // Method that calculates the average temperature in all the cities.
     private int calculateAverage ()
     {
          int sum = 0, average = 0;
          for (int count = 0; count < numberCities; count ++)
               sum = sum + temperatures [count].getAverage ();
          if (numberCities > 0) average = sum / numberCities;
          return average;
     } // method calculateAverage
 
     // Method that displays weather statistics for the cities.
    public void displayStatistics ()
    {
          int average = calculateAverage ();
          System.out.println ("The average temperature was " + average);
     } // method displayAverage
} // class CitiesWeather
 

Contents of citiesWeather file.
Atlanta 71 62
Boston 59 45
Chicago 66 49
Detroit 58 42
Edmonton 52 44
Fargo 63 52
Gainsville 73 5