// This program inputs the gender and returns an // array of all objects having that gender. import java.util.Scanner; import java.io.FileReader; import java.io.FileNotFoundException; public class FindGender { public static void main(String[ ] argv) throws FileNotFoundException { final int SIZE = 20; Person[ ] p = new Person[SIZE]; String theName = ""; char theGender = '\0'; int theAge = -1; Scanner s = new Scanner(new FileReader("persons.txt")); // Create Person array from file. for(int i = 0; i < p.length && s.hasNextLine( ); i++) { theName = s.next( ); theGender = s.next( ).charAt(0); theAge = s.nextInt( ); p[i] = new Person(theName, theGender, theAge); } // Print original array p. for(int i = 0; i < p.length && p[i] != null; i++) System.out.println(p[i]); // Input gender. Scanner con = new Scanner(System.in); System.out.print("Enter gender to find: "); char genderToFind = con.next( ).charAt(0); // Create new array for output. Person[ ] q = new Person[p.length]; // Put all Person objects of given gender in array q. int genderIndex = 0; for(int i = 0; i < p.length && p[i] != null; i++) if (p[i].getGender( ) == genderToFind) q[genderIndex++] = p[i]; // Print output array q. for(int i = 0; i < q.length && q[i] != null; i++) System.out.println(q[i]); } }