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(); 


} 

}

Recommended Answers

All 5 Replies

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.

can't me to do this..(String)names?

No, because an Enumeration and a String are completely different things; you can't just cast one to the other.

ok..thanks.i'll try....

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));
       } 
    }
}
commented: Post is relevant and shows how to avoid legacy data structures. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.