0

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


} 

}
3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by dreamslct
Featured 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 … Read More

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

2

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 by JamesCherrill: ps

1

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

2

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.