// Project: Patterns2 // Module: iterator // Source code file: Test.java // Illustrate the Iterator software pattern. // The iterator returns the pieces of state // information sequentially. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Test { public static void main(String[] args) throws FileNotFoundException { File f = new File("state-info.txt"); Scanner s = new Scanner(f); String line, state = "", capitol, largestCity, stateBird, stateFlower; String[ ] fields = null; String selectedState = "Illinois"; // Read and throw away first two lines. s.nextLine( ); s.nextLine( ); while (s.hasNextLine( )) { line = s.nextLine( ); fields = line.split(","); state = fields[0].trim( ); if (state.equals(selectedState)) { break; } } // Read state info from fields array. capitol = fields[1].trim( ); largestCity = fields[2].trim( ); stateBird = fields[3].trim( ); stateFlower = fields[4].trim( ); //Create StateInfo object. StateInfo info = new StateInfo(state, capitol, largestCity, stateBird, stateFlower); // Print state info using Iterator methods. while(info.hasNext( )) { System.out.println(info.next( )); } } }