import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.TreeSet;

public class UniqueWords
{
    private static final String FILE_NAME = "GettysburgAddress.txt";
    private static final int WORDS_PER_LINE = 8;
    
    public static void main(String args[])
    {
        Scanner in = null;
        
        try {
            in = new Scanner(new File(FILE_NAME));
            in.useDelimiter("[^A-Za-z]");
            
            TreeSet<String> words = new TreeSet<String>();
            
            while (in.hasNext()) {
                String word = in.next().toLowerCase();
                if (word.length() > 0) words.add(word);
            }
            
            int counter = 0;
            
            for (String word : words) {
                System.out.print(word);
                
                if (++counter < WORDS_PER_LINE) System.out.print(" ");
                else {
                    counter = 0;
                    System.out.println();
                }
            }
        }
        catch (FileNotFoundException ex) {
            System.out.println("*** File not found: " + FILE_NAME);
        }
        finally {
            if (in != null) in.close();
        }
    }
}
