/**
 * Reads in simple addresses from a user specified file.
 * Simple address fit the following format:
 *     [firstName] [lastName]
 *     [streetNumber] [streetStr]
 *     [city], [state (2-letters)] [zipCode (5 digits)]
 * 
 * @author Hyrum D. Carroll
 * @version 0.2 (April 16, 2019)
 */
import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;

public class AddressParser{

    public static void main( String[] args ){

        // Overview
        // Prompt user for file name
        // Get filename
        // Try to open the file
        // Read all valid addresses (one field at a time)
        // Close file

        Scanner stdinScanner = new Scanner( System.in ); // user's input
        String filename; // user's filename
        Scanner addressScanner = null;  // Scanner for filename

        // temp address variables
        String firstName;
        String lastName;
        int streetNumber; // also known as house number
        String streetStr;
        String city;
        String state;  // 2-letter abbreviation
        int zipCode;   // only 5 digits

        try{
            // Prompt user for file name
            // Get filename
            System.out.print("Please enter the output filename: ");
            filename = stdinScanner.next();

            // Try to open the file
            addressScanner = new Scanner( new FileReader( filename ) );

            // Read all valid addresses (one field at a time)
            while( addressScanner.hasNext() ){
                // Read address fields one by one
                firstName = addressScanner.next();
                lastName = addressScanner.next();
                streetNumber = addressScanner.nextInt();
                streetStr = addressScanner.nextLine();
                city = addressScanner.next();
                state = addressScanner.next();
                zipCode = addressScanner.nextInt();

                // Display complete address
                System.out.println("Found address: \"" + firstName + " " + lastName + ", "
                                   + streetNumber + streetStr + ", " + city + " "
                                   + state + " " + zipCode + "\"");
            }
        }catch( FileNotFoundException e ){
            System.err.println("Sorry, that was not a valid filename: " + e.getMessage() );
            return;
        }catch( InputMismatchException e){
            System.err.println("Error, unable to parse input!" );
            return;
        }catch( Exception e){
            System.err.println("ERROR: " + e.getMessage() );
            return;
        }finally{
            // Close file
            if( addressScanner != null ){
                addressScanner.close();
            }
        }
    }
}