   import java.io.*;
   import java.util.*;

/**********************************************************************
 * Class purpose: Print the current date and then read data from a text
 * file.  A message will be printed if it is matched in the text file
 * to whatever the current day is.
 *
 * @author David Petri
 * @version V1 1/22/07
 * Lab 4
 * Section 2
 *********************************************************************/
   public class IO_petridw_Remember
   {
      private static Scanner        dateScanner;
      private static PrintWriter    screen;
    
   
   /******************************************************************
   * Method purpose: Main method for a program that prints the current
	* date and other messages for the current day that are found in
	* text files.
   *
   * @param args Array of String elements that are the file names run
	* by the program.
    *****************************************************************/   
      public static void main(String[] args)
      {
         GregorianCalendar        today;
         String                   name;       
         File[]                   dates;

         dates    = new File[args.length];
         today    = new GregorianCalendar();
         screen   = new PrintWriter(System.out);
      	
         screen.printf(Locale.US, "\nToday's Date: %1$te %1$tB %1$tY\n", today);
         screen.flush();  
         
			//Since multiple files need to be read, a for loop is required that will iterate the same
			//number of times as the number of files that need to be read.
         for (int ii = 0; ii < args.length; ii++)
         {
			
				//Each separate file of the array must be instantiated
            dates[ii] = new File(args[ii]);
				
				//a FileNotFoundException try-catch block will be used in order to prevent
				//this exception from occurring.
            try
            {
               dateScanner = new Scanner(dates[ii]);
            }
            catch (FileNotFoundException fnfe)
            {
               screen.println("\nThe file '" + dates[ii] + "' could not be found.\n");
               screen.flush();
            }
            
				//a NullPointException try-catch block is in place here in order to prevent the program
				//from crashing in the case that only 1 file name is entered and that name is incorrect.
            try
            {
					//processDates will be run each time the for loop iterates
               processDates(today);
            }
            catch (NullPointerException npe)
            {
               screen.println("The file name was incorrect - the program will now end.");
					screen.flush();
            }
         }
			
			screen.println("The program will now end normally.");
			screen.flush();
      }//End main
    
   
     /******************************************************************
     * Method purpose: Finds & prints the messages for the current day
	  * from the specified text files.
     *
     * @param today GregorianCalendar attribute for the current day.
      *****************************************************************/
      private static void processDates(GregorianCalendar today)
      {
         int           day, month;              
         String        name;
       
      
       // Use the '\t' (tab) and '/' characters as delimiters
         dateScanner.useDelimiter("[\t/]");
          
          
         while (dateScanner.hasNext())
         {
            month = dateScanner.nextInt();
            day   = dateScanner.nextInt();
            name  = dateScanner.nextLine();  //Message attached to a day in the text file
         
            if ((month == (today.get(Calendar.MONTH)+1)) && 
              (day   == today.get(Calendar.DAY_OF_MONTH)))
            {
               screen.printf("%s\n", name);
               screen.flush();
            }
         }
      }//End ProcessDates
    
   
   }//End class
