Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default 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:

    Java Code:
    Map m = new HashMap();
        m.put("Word","def");
    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.
    Last edited by sehudson; 09-20-2011 at 06:16 PM.

  3. #3
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default 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?

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Limited word Dictionary

    you only need 1 map, just continue adding the key/value pairs to it.

  5. #5
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Limited word Dictionary

    so...
    Map m = new HashMap();
    m.put("Word1","def1","Word2","def2","Word3","def3" )


    or what?

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default 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.

  7. #7
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Limited word Dictionary

    So how would I call the strings back after the program runs?

    System.out.print(Word1 + def1);

    or what?

Similar Threads

  1. Replies: 1
    Last Post: 08-06-2011, 02:17 AM
  2. jtable showing limited rows
    By pink123 in forum AWT / Swing
    Replies: 6
    Last Post: 04-26-2011, 06:29 PM
  3. dictionary
    By aizen92 in forum New To Java
    Replies: 49
    Last Post: 01-01-2011, 09:07 AM
  4. Replies: 1
    Last Post: 10-01-2009, 01:51 PM
  5. add dictionary
    By monir6464 in forum New To Java
    Replies: 2
    Last Post: 04-07-2008, 06:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •