/**
 * ProcessFile.java
 * 
 * Illustrates the use of a Scanner to read a file one line at a time,
 * counting the number of lines.
 * 
 * For a sample input file, see romeo.txt
 * 
 * To count the number of words, we would need to change: 
 *     hasNextLine to hasNext
 *     nextLine to next
 */ 

import java.util.*;  // needed for Scanner
import java.io.*;    // needed for File

public class ProcessFile {
    //
    // **** IMPORTANT: We need to add the throws clause shown 
    //      in the method header below whenever we use a Scanner 
    //      to read from a file.
    //
    public static void main(String[] args) throws FileNotFoundException {
        // This Scanner reads from the console, so that we can
        // get the name of the file from the user.
        Scanner console = new Scanner(System.in);
        System.out.print("Name of file: ");
        String fileName = console.next();

        // This Scanner reads from the file.
        // We create a File object for the file and pass it 
        // immediately to the Scanner constructor.
        Scanner input = new Scanner(new File(fileName));
        
        int count = 0;
        while (input.hasNextLine()) {
            input.nextLine();  // read line and throw away
            count++;
        }

        System.out.println("The file has " + count + " lines.");
    }
}
