/home/wpollock1/public_html/AJava/Person.java
// This is simplified code (an outline really) to read a file
// and build a List of "Person"s. The file format for this is
// also simple: one person record per line, each record has
// two fields separated with a space: an int ("ID number") and
// a String (a single word "name").
//
// Written 4/2009 by Wayne Pollock, Tampa Florida USA
import java.io.*;
import java.util.*;
public class Person {
private final static String FILE = "People.txt";
private static List<Person> people = new ArrayList<Person>();
private int IDnum;
private String name;
public Person ( int id, String name ) {
IDnum = id;
this.name = name;
}
public String toString () {
return "ID " + IDnum + ": " + name;
}
public static void main ( String [] args ) {
Scanner data = null;
try { data = new Scanner( new File (FILE) ); }
catch ( FileNotFoundException e ) {
System.err.println( "File \"" + FILE + "\" Not Found!");
System.exit(1);
}
int IDnum;
String name;
while ( data.hasNextInt() ) {
IDnum = data.nextInt();
name = data.next();
people.add( new Person(IDnum, name) );
}
data.close();
// Now let's prove we read in the data correctly:
System.out.println( people.size() + " records read:\n" );
System.out.println ( people );
}
}