Source of TryVector.java


  1: //TryVector.java (from Horton)
  2: //Reads a Vector of "stars" from the keyboard,
  3: //then displays them in sorted order.
  4: 
  5: import java.util.*;
  6: import java.io.*;
  7: 
  8: public class TryVector
  9: {
 10:     public static void main(String[] args)
 11:     {
 12:         Person aPerson;                  //A person object
 13:         Crowd filmCast = new Crowd();
 14: 
 15:         //Populate the cast
 16:         while(true) //Indefinite loop
 17:         {
 18:             aPerson = readPerson();      //Read in a film star
 19:             if(aPerson == null)          //If null obtained...
 20:             break;                       //We are done...
 21:             filmCast.add(aPerson);       //Otherwise, add to the cast
 22:         }
 23: 
 24:         int count = filmCast.size();
 25:         System.out.println("You added "+count +
 26:             (count == 1 ? " person":  " people")+ " to the cast.\n");
 27: 
 28:         filmCast.sort(); //Sort the cast
 29: 
 30:         //Show who is in the cast using an iterator
 31:         Iterator thisLot = filmCast.iterator();  //Obtain an iterator
 32:         while(thisLot.hasNext())                 //Output all elements
 33:             System.out.println( thisLot.next() );
 34:     }
 35: 
 36:     //Read a person from the keyboard
 37:     static public Person readPerson() 
 38:     {
 39:         FormattedInput in = new FormattedInput();
 40: 
 41:         //Read in the first name and remove blanks front and back
 42:         System.out.println("\nEnter first name or ! to end:");
 43:         String firstName = in.stringRead().trim(); //Read and trim string
 44: 
 45:         if(firstName.charAt(0) == '!')  //Check for ! entered
 46:             return null;                //If so, we are done
 47: 
 48:         //Read in the surname, also trimming blanks
 49:         System.out.println("Enter surname:");
 50:         String surname = in.stringRead().trim();   //Read and trim string
 51:         return new Person(firstName,surname);
 52:     }
 53: }
 54: