// An application with a class that reads information about people from
a file and displays it.
public class People
{
public static void main (String [] args)
{
ListClass people
= new ListClass ();
people.readList
();
people.displayList
();
people.displayOldest
();
} // main method
} // class People
class ListClass
{
private Person [] list;
private int listSize;
final int maxSize = 10;
ListClass ()
{
list = new Person
[maxSize];
listSize = 0;
} // constructor
public void readList ()
{
try
{
BufferedReader infile = new BufferedReader (new InputStreamReader (new
FileInputStream ("infile.txt")));
String name = infile.readLine ();
while ((name != null) && (listSize < maxSize))
{
Person person = new Person (name);
person.readData (infile);
list [listSize] = person;
listSize ++;
name = infile.readLine ();
}
infile.close ();
}catch (IOException
e) {System.out.println ("File Error.");}
catch (NumberFormatException
e) {System.out.println ("Number Error.");}
} // method readList
public void displayList ()
{
for (int count
= 0; count < listSize; count ++)
list [count].displayData ();
} // method displayList
public void displayOldest ()
{
int oldest = list [0].getAge
();
int locationOldest = 0;
for (int count = 1; count
< listSize; count ++)
{
if (list [count].getAge () > oldest)
{
oldest = list [count].getAge ();
locationOldest = count;
}
}
list [locationOldest].displayData
();
} // method displayOldest
} // class ListClass
// Class that stores information about a person's name and age.
class Person
{
private String name;
private int age;
Person (String name) { this.name = name; } // constructor
public int getAge () {return age;}
// A method to read data from a file.
It uses a buffered reader.
public void readData (BufferedReader infile)
throws IOException
{
age = Integer.parseInt
(infile.readLine ());
} // method readData
public void displayData ()
{
System.out.println
(name + "'s age is " + age);
} // method displayData
} // class Person