import java.lang.*; import java.util.*; import java.io.*; /** CloseWords: Exploit the clustering tendency of the native hashCode() method * in the String class to find words that are "close" to the argument. */ public class CloseWords { Hashtable ht; String currString; /** In the code below we create an instance of the Hashtable class in which to store * our hash of all of the words in the system dictionary (yes, this is a very memory * inefficient way of indexing the words). * * @param args */ public static void main(String[] args) { ht = new Hashtable(); try { DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("/usr/dict/words"))); while((currString = in.readLine())!=null) ht.put(new Integer(currString.hashCode()), currString); int i = args[0].hashCode(); int found=0; while(found < 5) { i--; if(ht.get(new Integer(i))!=null) { System.out.println(ht.get(new Integer(i))); found++; } } i = args[0].hashCode(); found=0; while(found < 5) { i++; if(ht.get(new Integer(i))!=null) { System.out.println(ht.get(new Integer(i))); found++; } } } catch(IOException ioe) { System.out.println("IO Exception"); } } }