Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Creating a dicitonary program This is my lab assignment... The goal of this lab

ID: 650072 • Letter: C

Question

Creating a dicitonary program

This is my lab assignment...

The goal of this lab will be to implement a dictionary using two files: one file called "dictionary_keys.txt" that will contain the words of your dictionary, and another file called "definitions.txt" that will contain the definitions for each word. The words are matched up to each definition on each line (i.e., the definition for the word on line 1 in dictionary_keys.txt should be mapped to the definition on line 1 of definitions.txt).

I am not sure if you guys have started learning about streaming files just, yet. In case you haven't I'll help you out a bit. You can use this code in order to open both of the files by declaring an ifstream object (in case your aren't familiar: http://www.cplusplus.com/reference/fstream/ifstream/). Then, you can use the getline() function from the stdio.h library to read a line into a string. Here is a snippet of code of that in action:

#include
#include
using namespace std;

int main(){

   ifstream ifs_definitions("definitions.txt");
   ifstream ifs_keys("dictionary_keys.txt");
   string key, def;
   while(getline(ifs_definitions, def) && getline(ifs_keys,key)){     
       //do something with the strings key and def
   }

   ifs_definitions.close();

   ifs_keys.close();

}

What this code will do is essentially read every line of both files until it runs out of lines to "get". Each line of the dictionary_keys.txt and defintion.txt files will be stored in the strings "key" and "def", respectively.

Once you have read the values into a std::map, you will prompt the user to enter one of the words in your dictionary (case-sensitive). Your program should then output the associated definition with the input string. You'll probably want to open the dictionary_keys.txt file to see what words are actually in your dictionary when testing the program.

Here is the output of a sample solution:

./dictionary
Please enter the word you would like to search in the dictionary (enter exit to quit): Apple

   Apple: A fruit used to make apple sauce and pies.

Please enter the word you would like to search in the dictionary (enter exit to quit): Cat

   Cat: A domestic feline.

Please enter the word you would like to search in the dictionary (enter exit to quit): Kitten

   Kitten: A baby cat.

Please enter the word you would like to search in the dictionary (enter exit to quit): Zebra

   Zebra: A horse with white and black stripes.

Please enter the word you would like to search in the dictionary (enter exit to quit): asdf

No match found in the dictionary
Please enter the word you would like to search in the dictionary (enter exit to quit): exit

Explanation / Answer

Answer

import java.io.*;

class dictionary
{
    publicstaticvoid main(String args[])
    {
        int i;
        boolean flag = false;
        String sub = new String();
        String strf = new String();

        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String str = br.readLine();
            FileReader fr = new FileReader("dictionaryTXT.txt");
            BufferedReader bb = new BufferedReader(fr);
          
            while((strf = bb.readLine()) != null)
            {
                i = strf.indexOf(":");
                sub = strf.substring(0,i);
                if(str.equals(sub))
                {
                    System.out.println(strf);
                    flag=true;
                }
      
            }
            bb.close();

            if(!flag)
                System.out.println("Word not found");

        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

-----------------------------------------

output:

E:>java dictionary
daring
daring:courage.

E:>java dictionary
Vidyadhar
Word not found

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote