// Computer Science 121
// Example that reads data from a file and stores it in an array of classes

import java.io.*;

// An application with a class that provides age statistics about some people.
class AgeStatistics
{
     public static void main (String [] args)
     {
          People people = new People ();
          people.readData ();
          people.displayData ();
     } // main method
} // class AgeStatistics

// Class that stores information about a person's name and age.
class Person
{
     private String name;
     private int age;

    public int getAge ()
    {
        return age;
    } // getAge

     // A method to read data from a file and store it in the class.
     public void readPerson (BufferedReader infile, String n) throws IOException
     {
           name = n;
           age = Integer.parseInt (infile.readLine ());
     } // method readPerson

 // A method that displays name and age information about a person.
 public void displayPerson ()
 {
  System.out.println (name + "'s age is " + age);
 } // method displayPerson
} // class Person

    // A class that manages an array of classes.  Each one contains a person’s name and age.
    class People
    {
         private Person [] list = new Person [10];
         private int noPeople = 0;

     // A method that reads data from a file and stores it in an array of classes.
     public void readData ()
     {
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("infile.txt")));
               String name = infile.readLine ();
               while (name != null)
               {
                    Person person = new Person ();
                    person.readPerson (infile, name);
                    list [noPeople] = person;
                    noPeople ++;
                    name = infile.readLine ();
               }
          }
          catch (IOException e) { System.out.println ("I/O error");}
          catch (NumberFormatException e) { System.out.println ("Number format error.");}
     } // readData

     // A method that displays information about all the people in the array.
     public void displayData ()
     {
          for (int count = 0; count < noPeople; count ++)
               list [count].displayPerson ();
          int oldest = findOldest ();
          System.out.println ("The oldest person is " + oldest + " years old.");
     } // displayData

    // A method that searches the array to find the oldest person.
    private int findOldest ()
    {
        int oldest = list [0].getAge ();
        for (int count = 0; count < noPeople; count ++)
        {
            if (list [count].getAge () > oldest)
                oldest = list [count].getAge ();
        }
        return oldest;
    } // findOldest
} // class People

infile.txt

Alice Lee
7
Bobby Smith
8
Cathy Levin
7
Daniel Chen
4