/**
 * Implements an anagram generator
 */
import java.io.*;
import java.util.*;

public class Anagrams
{
	private WordCollection w = new WordCollection();
	

	public Anagrams(BufferedReader file) throws IOException
	{
		String s;
		
		while ((s = file.readLine()) != null)
		{
			StringTokenizer sTok = new StringTokenizer(s);
			while (sTok.hasMoreTokens())
			{
				String word = sTok.nextToken();
				w.addWord(word);
			}
		}
		file.close();
	}
	

	public void generateAnagrams() throws IOException
	{
		String searchKey;
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	  	do
	  	{
	  		System.out.print("\nletters to search [#] to stop: ");			
			searchKey = input.readLine();
			if (!searchKey.equals("#"))
	  		{
	  			w.printMatches(searchKey);
	  		}
		}
		while (!searchKey.equals("#"));
	}
	
	
	public static void main(String[] args) throws IOException
	{
		Anagrams a = new Anagrams(new BufferedReader(new FileReader("small-words.txt")));
		a.generateAnagrams();
	}
}