/**
 * Reads in simple addresses from a user specified file.
 * Simple address fit the following format:
 *     [line1]
 *     [line2]
 *     [line3]
 * Addresses need to be separated by a blank line.
 * 
 * @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 AddressParserSimple{

    public static void main( String[] args ){

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

        Scanner stdinScanner = new Scanner( System.in ); // user's input
        String filename; // user's filename
        BufferedReader reader =  null;  // file reader for user's supplied filename

        // temp address variables
        String line1;
        String line2;
        String line3;

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

            // Try to open the file
            reader = new BufferedReader( new FileReader( filename ) );

            // Read all valid addresses (3 lines at a time plus 1 blank one)
            line1 = reader.readLine();
            while( line1 != null ){
                line2 = reader.readLine();
                line3 = reader.readLine();
                reader.readLine();          // for blank line

                // Display complete address
                System.out.println("Found address: \"" + line1 + ", " + line2 + ", " + line3 + "\"" );

                line1 = reader.readLine();
            }
        }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(reader != null ){
                try{
                    reader.close();
                }catch( Exception e ){
                    System.err.println("Error closing reader: " + e.getMessage());
                }
            }
        }
    }
}