Results 1 to 7 of 7
Thread: Limited word Dictionary
- 09-20-2011, 05:57 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Limited word Dictionary
Here's the program instructions.
Ask the user to create a small dictionary consisting of words and their meanings - at least 5 words. Store the words and their meanings in a suitable construct. Provide the following additional functionality to the user:
a)Ask the user for a word. Search for the word in the dictionary. If the word exists, print the meaning of the word, else print the message - "The word does not exist in the dictionary!"
I have the pseudo code, but I don't know how to store the strings and recall them or how to search for matches.
Pseudo:
Prompt the user for 5 words + definitions.
Store the words and definitions.
Ask the user to enter a word.
If the word is one of the 5 previously entered words, then display that word with the definition
else, display the message "The word does not exist in the dictionary!"
I know how to ask the user to enter the words, definition, I don't know what the best way to store them would be. I can ask the user to enter a word to search for, but don't know how to search for a word through the previously entered strings.
- 09-20-2011, 06:12 PM #2
Re: Limited word Dictionary
I would use a hash map to store the word and the definition.
With a hashmap, you create key value pairs, and so your key would be the word, and the value would be the definition of the word, for example:
So you would add a new key/value pair for each word/definition that you want to store. That would be a good starting point, then look at the methods associated with a map and think about what you can use to search the map and determine if the word the user enters is there.Java Code:Map m = new HashMap(); m.put("Word","def");Last edited by sehudson; 09-20-2011 at 06:16 PM.
- 09-20-2011, 08:33 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: Limited word Dictionary
So how would I do that with multiple words and definitions?
like:
Map m = new HashMap();
m.put("Word1","def1");
for the first, but for the second would I have to declare a new Map?
Map n = new HashMap();
n.put("Word2", "def2");
or how would I do this?
- 09-20-2011, 08:45 PM #4
Re: Limited word Dictionary
you only need 1 map, just continue adding the key/value pairs to it.
- 09-20-2011, 10:14 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: Limited word Dictionary
so...
Map m = new HashMap();
m.put("Word1","def1","Word2","def2","Word3","def3" )
or what?
- 09-20-2011, 10:20 PM #6
Re: Limited word Dictionary
Think about what you're trying to do.
Once you create the map,
Java Code:Map m = new HashMap();
if you type "m." you are presented with a list of methods available for a Map. This can really help you understand each method, the arguments that it take, and the type of Object returned, if any.
You will notice you have a put method that accepts 2 Objects as arguments, one being the key, and the other being the value.
So that means you add 1 key/value pair at a time, i.e.
Java Code:m.put("Word1","def1"); m.put("Word2,"def2"); .... ....Last edited by sehudson; 09-20-2011 at 10:32 PM.
- 09-21-2011, 01:17 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Advise.. What to do when you have too much limited time for a project
By Nazneen Ali in forum New To JavaReplies: 1Last Post: 08-06-2011, 02:17 AM -
jtable showing limited rows
By pink123 in forum AWT / SwingReplies: 6Last Post: 04-26-2011, 06:29 PM -
dictionary
By aizen92 in forum New To JavaReplies: 49Last Post: 01-01-2011, 09:07 AM -
Creating many Sessions for limited USers in Hibernate
By SE123 in forum JDBCReplies: 1Last Post: 10-01-2009, 01:51 PM -
add dictionary
By monir6464 in forum New To JavaReplies: 2Last Post: 04-07-2008, 06:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks