
/**
*
*  This code was written to show what happens when a user instantiates an
*  empty file and tries to read from it.  Note that file is instantiated as
*  long is it exists.  However, when the Scanner tries to read from it, that
*  fails with a "no such element exception ".
*
*  @author Elizabeth Adams
* 
*
*/

import java.util.Scanner;
import java.io.*;

public class TestEmptyFile
{
  public static void main (String [] args)
  {
     Scanner myScanner;
	  String fileName;
	  String temp;

	  fileName = "empty.txt"; // want an empty file of this name in directory
	  
	  try
	  {  
	 	  myScanner = new Scanner (new File (fileName)); // no problem here
	  }
	  catch (Exception e)  // in case file "empty.txt not in directory  
	  {
	    myScanner = new Scanner ("pacifier for compiler"); // don't expect to use this
	    System.out.println (e.getMessage());
		 e.printStackTrace();
	  } 	   
	 
	  try
	  { 
	  temp = myScanner.next();  // this fails because file is empty
	  System.out.println (temp);  
	  System.out.println (" no problem ");
	  }
	  catch (java.util.NoSuchElementException nsee)
	  {
	    System.out.println (nsee.getMessage());  // prints null
		 nsee.printStackTrace(); /* prints
			java.util.NoSuchElementException
   		at java.util.Scanner.throwFor(Scanner.java:817)
	   	at java.util.Scanner.next(Scanner.java:1317)
			at TestEmptyFile.main(TestEmptyFile.java:26)
		 */
	  }	
	     
	}
}