// SetDemo.java
// by Scot Drysdale on 11/25/04
// modified to use Scanner and generics on 5/21/08

// Demonstrates the use of a set to determine if input words are 
// Java reserved words.


import java.util.*;

public class SetDemo {

	public static void main(String[] args) 
	{
		String [] reservedArray = {"abstract", "boolean", "break", "byte",
				"case", "catch", "char", "class", "const", "continue", "default",
				"do", "double", "else", "extends", "false", "final", "finally",
				"float", "for", "goto", "if", "implements", "import", "instanceof",
				"int", "interface", "long", "native", "new", "null", "package",
				"private", "protected", "public", "return", "short", "static",
				"strictfp", "super", "synchronized", "this", "throw", "throws",
				"transient", "true", "try", "void", "volatile", "while"};
		Set<String> reservedWords;			// Set of all Java reserved words

		reservedWords = new HashSet<String>();		// Also try TreeSet
		Scanner input = new Scanner(System.in);

		// Put all reserved words into the set reservedWords
		for (int i = 0; i < reservedArray.length; i++)
			reservedWords.add(reservedArray[i]);

		// Print all the reserved words
		System.out.println("The Java reserved words are:");
		Iterator<String> iter = reservedWords.iterator();
		while(iter.hasNext())
			System.out.println(iter.next());


		while(true)
		{
			System.out.print("Enter a word: ");
			String word = input.nextLine();

			if(reservedWords.contains(word))
				System.out.println(word + " is a reserved word.\n");
			else
				System.out.println(word + " is not a reserved word.\n");
		}
	}
}
