Java Reference
In-Depth Information
import java.util.*;
import java.io.*;
public class TryVector {
public static void main(String[] args) {
Person aPerson; // A person object
Crowd filmCast = new Crowd();
// Populate the crowd
for( ; ; ) { // Indefinite loop
aPerson = readPerson(); // Read in a film star
if(aPerson == null) // If null obtained...
break; // We are done...
filmCast.add(aPerson); // Otherwise, add to the cast
}
int count = filmCast.size();
System.out.println("You added " + count +
(count == 1 ? " person": " people") + " to the cast.\n");
// Show who is in the cast using an iterator
Iterator thisLot = filmCast.iterator(); // Obtain an iterator
while(thisLot.hasNext()) // Output all elements
System.out.println( thisLot.next() );
}
}
Note the two import statements at the beginning. The first is needed for the Iterator in main() .
The other is used for the BufferedReader and InputStreamReader objects in the readPerson()
method. Let's now add this method to the TryVector class:
// Read a person from the keyboard
static public Person readPerson() {
FormattedInput in = new FormattedInput();
// Read in the first name and remove blanks front and back
System.out.println(
"\nEnter first name or ! to end:");
String firstName = in.readString().trim(); // Read and trim a string
if(firstName.charAt(0) == '!') // Check for ! entered
return null; // If so, we are done...
// Read in the surname, also trimming blanks
System.out.println("Enter surname:");
String surname = in.readString().trim(); // Read and trim a string
return new Person(firstName,surname);
}
Search WWH ::




Custom Search