This is the code which i wrote.But when i input a word,it should search it and give the definition.I think there is a problem in while loop.But i can't solve it.Can u guys,plz check it?

import java.util.*; 

class dictionary
{
 
public static void main(String args[])

{
 
Hashtable dict = new Hashtable(); 

Enumeration names; 

String str; 

String n;

dict.put("discuss", new String("talk about something")); 

dict.put("encourage", new String("persuade to do")); 

dict.put("thread", new String("a thing string of cotton")); 

 

// Show all words in hash table. 

names = dict.keys(); 

System.out.println("Enter a word:"+args[0]);

n=args[0];

if(names.equals(n))
{

while(names.equals(n)) 
{ 

str = (String)names.nextElement(); 

System.out.println(str+ ": " +dict.get(str)); 

} 
}

System.out.println(); 


} 

}

if(names.equals(n))

names is an Enumeration

n is args[0] which is a String.

An Enumeration and a String can never be equal.

ps: Hashtable is designed for this kind of thing, so it has other methods that you will find very useful... spend 10 minutes reviewing all its methods before choosing which to use.

Edited 4 Years Ago by JamesCherrill: ps

Hi pro learner ,

Just logged in and see your post about creating a simple dictionary. Though it is a dead post but posting in case you still wishing to have fun with it. Here is some code might help you out.

 import java.util.*;
public class Dictionary {


    public static void main(String[] args) {

        Map <String, String> dictionary;
        dictionary = new TreeMap <String , String>();
        Scanner s = new Scanner (System.in);
        dictionary.put("discuss", "talk about something");
        dictionary.put("fissure", "a narrow opening");
        dictionary.put("spasmodic", "sudden but for short time");
        System.out.println("Enter a word :");
        String search = s.next();



       if(dictionary.containsKey(search)){

       System.out.println(dictionary.get(search));
       } 
    }
}
Comments
Post is relevant and shows how to avoid legacy data structures.
This article has been dead for over six months. Start a new discussion instead.