Dictionary Application Using Java

The main aim is to design a dictionary using Java Development Kit. The dictionary will contain list of simple words, wherein the user will find the meaning of the word along with the usage (i.e. an example sentence). The user will also be able to suggest the meaning for a particular word along with its usage. This suggested word will not be added immediately but it will be given to the administrator where he/she will verify if the entered meaning is correct or not and if the meaning is correct the word will be added to the dictionary.

Steps to Execute the dictionary application.

Prerequisites:You must have windows operating system and Microsoft access 2007 installed on your computer.You must have java development kit(jdk) installed. Your class path must be updated to "jdk\bin"
directory

Steps:

  1. Unzip the .zip file and copy the database file (.accdb) to a location of your choice. Then set
    up a ODBC connection to this point. This can be done  as follows 
    • Go to "Control Panel->Administrative Tools-> Data Source(ODBC)" 
    • Click on the "System DSN" tab.  
    • Click Add button  
    • In the ensuing dialog box, select "Microsoft Access Driver(*.mdb,*.accdb)"  
    • Select ok.  
    • In the following dialog box give the dsn name as "mydsn" and click on "select" button.  
    • Select the location where you stored the  database.  
    • click ok.  
    • Click ok.
  2. From the unzipped contents, copy the two .class files to your desktop.
  3. Open the command prompt. To do this go to the start menu and open "run" dialog box. Enter "cmd" in the dialog box. Press enter. The command prompt will open on your desktop.
  4. 4) In the command prompt, type "cd Desktop" and  press enter.
  5. 5) Now enter "java -cp . DictionarySwitch" and press enter
  6. 6) You will see the main menu of the dictionary application.
  7. 7) From here you can navigate to any place using  the menu options displayed. It is important that you remember to press the "Enter key" after every operation you perform(search, add , delete, etc.)
    so that the menu is displayed once again and you  can perform your next task in the application.

NOTE: admin id is 'admin'
admin password is 'adminpassword'

Source Code:  AdminSwitch.java

import java.sql.*;
import java.io.*;
public class AdminSwitch
{
public static boolean admin(boolean grant) throws IOException,SQLException
{
    System.out.println(grant);
    if(grant==false)
    return grant;
    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;
    String query;
    grant=false;
    boolean flag=false;
    boolean dummy;
   
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//for(;;)
//{
    for(int i=0;i<50;i++)
        System.out.println();
//spacing
    while(true)
    {
    for(int i=0;i<10;i++)
        System.out.println();
    System.out.println("you are logged in as administrator");
    System.out.println();
    System.out.println("1.View and edit user suggestions");
    System.out.println("2.add/delete from dictionary");
    System.out.println("3.logout");
    System.out.println();
    System.out.print("Enter your choice:");
    String s=br.readLine();
    int choice=Integer.parseInt(s);
    switch(choice)
    {
    case 1:
//suggesstions   
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(Exception e)
        {
            System.out.println("Driver class not found");
        }
        try
        {
            con=DriverManager.getConnection("jdbc:odbc:mydsn");
        }
        catch(Exception ee)
        {
            System.out.println("Cannot make connection");
        }
        try
        {
   
            stmt=con.createStatement();
        }
        catch(Exception eee)
        {
            System.out.println("cannot create statement");
        }
        query="Select * from at";
        try
        {
            rs=stmt.executeQuery(query);
        }
        catch(Exception ax)
        {
            System.out.println("some problem while executing query");
        }
   
        try
        {s=null;
            while(rs.next())
            {
                s=rs.getString("word");
                System.out.println("Word: "+s);
                System.out.println("meaning: "+rs.getString("meaning"));
                System.out.println("usage: "+rs.getString("usage"));
                System.out.println();
                System.out.println();   
                flag=true;   

            }
        }   
        catch(Exception axx)
        {
            System.out.println("rs.next() has some exception");
        }
try
{
        if(!s.equals(null))
        {
            System.out.println();
            System.out.println();
            System.out.println("\t\t\t\t\tSuggestions menu");
            System.out.println();
            System.out.println("1.Add one of these words to dictionary");
            System.out.println("2.Delete a suggested word");
            System.out.println("3.Return to main admin menu");
            System.out.println("enter your choice");

            s=br.readLine();
            choice=Integer.parseInt(s);
            System.out.println();
            switch(choice)
            {
            case 1:
                System.out.println("Enter a word");
//add suggestion to dictionary
                String word=br.readLine();
                //int loop=1;
                String newWord=null;
                String newMeaning=null;
                String newUsage=null;
                        query="Select * from at where word='"+word.toLowerCase()+"'";
                        try
                        {
                            rs=stmt.executeQuery(query);
                        }
                        catch(Exception exc)
                        {
                            System.out.println("result set not got when selecting from at");
                        }
                        try
                        {    //s=null;
                            newWord=null;
                            while(rs.next())
                            {
               
                                newWord=rs.getString("word");
                                newMeaning=rs.getString("meaning");
                                newUsage=rs.getString("usage");
                                //s=newWord;
                            }
                        }
                        catch(Exception tion)
                        {
                            System.out.println("exception While executing rs.next() for newword");
                        }
                        try
                        {
                            if(word.equalsIgnoreCase(newWord))
                            {
                                String x="','";
                                String y="')";
                                query="insert into dt(word,meaning,usage) values('"+newWord.toLowerCase()+x+newMeaning+x+newUsage+y;
                                try
                                {
                                    stmt.execute(query);
                                    System.out.println("Dictionary updated");
                                    s=br.readLine();
                                }
                                catch(Exception yxd)
                                {
                                    System.out.println("exception while inserting into database");
                                }
                                query="delete from at where word='"+newWord.toLowerCase()+"'";
                                try
                                {   
                                    stmt.execute(query);
                                }
                                catch(Exception eed)
                                {
                                    System.out.println("exception while deleting from database");
                                }
                            }
                            else
                            {
                                System.out.println("The word you entered is not present in database");
                                s=br.readLine();

                            }
                        }
                        catch(Exception aaoght)
                        {
                            System.out.println("The word you entered is not present in database");
                        }
                        break;
            case 2:
//delete a suggested word
                System.out.println();
                System.out.println("Enter the word you want to delete");
                newWord=br.readLine();
                System.out.println("Do you really want to delete the string? Enter yes/no.");
                s=br.readLine();
                s=s.toUpperCase();
                if(s.equals("YES"))
                {
                    query="select * from at where word='"+newWord.toLowerCase()+"'";
                    try
                    {
                        rs=stmt.executeQuery(query);
                    }
                    catch(Exception eep)
                    {
                        System.out.println("some problem in getting the result set");
                    }
                    try
                    {
                        s=null;
                        while(rs.next())
                        {
                            s=rs.getString("word");
                        }
                    }
                    catch(Exception aaoo)
                    {
                        System.out.println("exception while processing result set");
                    }
                    try
                    {
            //word=word.toUpperCase();
                        s=s.toUpperCase();
                        if(s.equals(newWord.toUpperCase()))
                        {
                            query="Delete from at where word='"+newWord.toLowerCase()+"'";
                            try
                            {
                                stmt.execute(query);
                                System.out.println("Deletion from suggestions list successful");
                                s=br.readLine();
                            }
                            catch(Exception eex)
                            {
                                System.out.println("Exception while deleting from at");
                            }
                               
                        }
                        else
                        {
                            System.out.println("The word is not present in the database to delete");
                            s=br.readLine();
                        }       
                    }
                    catch(Exception aeef)
                    {
                        System.out.println("The word is not present in the database to delete");
                    }
                   
                }
               
                break;
            default:
                System.out.println("You have chosen not to modify anything");
            }
        }
}
        catch(Exception rres)
        {
            System.out.println("there are no new suggestions");
            s=br.readLine();
        }
        stmt.close();
        con.commit();
        con.close();
        break;

/*                                             case 2                        */

 

    case 2:
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(Exception e)
        {
            System.out.println("Driver class not found");
        }
        try
        {
            con=DriverManager.getConnection("jdbc:odbc:mydsn");
        }
        catch(Exception ee)
        {
            System.out.println("Cannot make connection");
        }
        try
        {
   
            stmt=con.createStatement();
        }
        catch(Exception eee)
        {
            System.out.println("cannot create statement");
        }
       
        System.out.println("\t\t\t\tDictionary modification menu");
        System.out.println("1.add word to dictionary");
        System.out.println("2.delete word from dictionary");
        System.out.println("3.Return to admin main menu");
        System.out.println("Enter 1,2 or 3");
        s=br.readLine();
        choice=Integer.parseInt(s);
        switch(choice)
        {
        case 1:
           
            System.out.println("Enter a word");
                    //add suggestion to dictionary by admin
            String word=br.readLine();
            String newWord=null;
            String newMeaning=null;
            String newUsage=null;
            query="Select * from dt where word='"+word.toLowerCase()+"'";
            try
            {
                rs=stmt.executeQuery(query);
            }
            catch(Exception exc)
            {
                System.out.println("result set not got when selecting from at");
            }
            try
            {    //s=null;
                //newWord=null;
                while(rs.next())
                {
                    newWord=rs.getString("word");
                }
            }
            catch(Exception tion)
            {
                System.out.println("exception While executing rs.next() for newword");
            }
            try
            {
                newWord=newWord.toUpperCase();
                if(newWord.equals(word.toUpperCase()))
                {
                    System.out.println("The word is already present in the dictionary");
                    s=br.readLine();
                }
            }
            catch(Exception cat)
            {
                newWord=word;
                System.out.println("Enter the meaning");
                newMeaning=br.readLine();
                System.out.println("Enter the usage");
                newUsage=br.readLine();
                String x="','";
                String y="')";
                query="insert into dt(word,meaning,usage) values('"+newWord.toLowerCase()+x+newMeaning+x+newUsage+y;
                try
                {
                    stmt.execute(query);
                    System.out.println("Word added to dictionary");
                    s=br.readLine();
                }
                catch(Exception yxd)
                {
                    System.out.println("exception while inserting into database");
                }
            }
            break;
        case 2:
            System.out.println();
            System.out.println("Enter the word you want to delete");
            newWord=br.readLine();
            System.out.println("Do you really want to delete the string? Enter yes/no.");
            s=br.readLine();
            s=s.toUpperCase();
            if(s.equals("YES"))
            {
                query="select * from dt where word='"+newWord.toLowerCase()+"'";
                try
                {
                    rs=stmt.executeQuery(query);
                }
                catch(Exception xeep)
                {
                    System.out.println("some problem in getting the result set");
                }
                try
                {
                    s=null;
                    while(rs.next())
                    {
                        s=rs.getString("word");
                    }
                }
                catch(Exception xaaoo)
                {
                    System.out.println("exception while processing result set");
                }
                try
                {
            //word=word.toUpperCase();
                    s=s.toUpperCase();
                    if(s.equals(newWord.toUpperCase()))
                    {
                        query="Delete from dt where word='"+newWord.toLowerCase()+"'";
                        try
                        {
                            stmt.execute(query);
                            System.out.println("Word deleted from dictionary");
                            s=br.readLine();
                        }
                        catch(Exception xeex)
                        {
                            System.out.println("Exception while deleting from dt");
                        }
                           
                    }   
                    else
                    {
                        System.out.println("The word is not present in the database to delete");
                        s=br.readLine();
                    }   
                }
                catch(Exception axeef)
                {
                    System.out.println("The word is not present in the database to delete");
                }
               
            }
            break;   
        default:
            //do nothing       
        }
        stmt.close();
        con.commit();
        con.close();       
        break;

 

/*                                              case 3                           */

    case 3:
       
        return grant;
    default:
        System.out.println("Choose option 1,2 or 3");
    }
    }

 

//
}
}

Source Code: DictionarySwitch.java

import java.io.*;
import java.sql.*;
public class DictionarySwitch
{
public static void main(String[] args) throws IOException,SQLException
{
    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;
    String query;
    boolean grant=false;

/*                                                    start of loop                            */

    for(;;)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(Exception e)
        {
            System.out.println("Driver class not found");
        }
        try
        {
            con=DriverManager.getConnection("jdbc:odbc:mydsn");
        }
        catch(Exception ee)
        {
            System.out.println("Cannot make connection");
        }
        try
        {

            stmt=con.createStatement();
        }
        catch(Exception eee)
        {
            System.out.println("cannot create statement");
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("\t\t\t\t\tWelcome to WORLD OF WORDS");
        System.out.println();
        System.out.println("Select one of the following menu options");
        System.out.println();
        System.out.println("1.Search for meaning");
        System.out.println("2.Suggest new word");
        System.out.println("3.Login as administrator");
        System.out.println("4.quit");
        System.out.println();
        System.out.println("Enter your choice");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s=(String)br.readLine();
        int choice=Integer.parseInt(s);
        System.out.println("choice is: "+choice);
        String word=null;
        String meaning=null;
        String usage=null;
    //switch begins
        switch(choice)
        {

/*                                                      case  1                                             */

        case 1:
            System.out.println("your choice is to search for the meaning of a word");
            System.out.println();
            System.out.print("Enter a word: ");
            String searchWord=br.readLine();
            System.out.println();
            System.out.println("You entered "+searchWord);
            query="Select * from dt where word='"+searchWord.toLowerCase()+"'";
            try
            {
                rs=stmt.executeQuery(query);
            }
            catch(Exception eeee)
            {
                System.out.println("some problem in gettin the result set");
            }
            try   
            {
                while(rs.next())
                {
                    word=rs.getString("word");
                    meaning=rs.getString("meaning");
                    usage=rs.getString("usage");
                }
            }
            catch(Exception eeeee)
            {
                System.out.println("The result set may be null or may be invalid");
            }
           
            try
            {
                boolean we=word.equals(null);
                System.out.println("Word: "+word+"\nMeaning: "+meaning+"\nUsage : "+usage);
            }
            catch (Exception tt)
            {
                System.out.println("The word was not found in the dictionary");   
               
            }
            s=br.readLine();
            stmt.close();
            con.commit();
            con.close();
            break;

/*                                                    case 2                                                    */

 

        case 2:
            System.out.println("your choice is to suggest a new word");
            System.out.println();
            System.out.print("Enter the word: ");
            word=br.readLine();
            boolean insert=true;
            query="select * from dt where word='"+word.toLowerCase()+"'";
            try
            {
                rs=stmt.executeQuery(query);
            }
            catch(Exception eep)
            {
                System.out.println("some problem in getting the result set");
            }
            try
            {
                s=null;
                while(rs.next())
                {
                    s=rs.getString("word");
                    insert=false;
                }
            }
            catch(Exception aaoo)
            {
                System.out.println("exception while processing result set");
            }
            try
            {
        //word=word.toUpperCase();
                s=s.toUpperCase();
                if(s.equals(word.toUpperCase()))
                {
                    insert=false;
                    System.out.println("The word exists in the database");
                }       
            }
            catch(Exception aeef)
            {
                query="select * from at where word='"+word.toLowerCase()+"'";
                try
                {
                    rs=stmt.executeQuery(query);
                }
                catch(Exception eepr)
                {
                    System.out.println("some problem in getting the result set");
                }
                try
                {
                    s=null;
                    while(rs.next())
                    {
                        s=rs.getString("word");
                        insert=false;
                    }
                }
                catch(Exception aaoor)
                {
                    System.out.println("exception while processing result set");
                }
                try
                {
            //word=word.toUpperCase();
                    s=s.toUpperCase();
                    if(s.equals(word.toUpperCase()))
                    {
                        insert=false;
                        System.out.println("The word exists in the database");
                    }       
                }
                catch(Exception aaeefr)
                {
                               
                    System.out.println();
                    System.out.print("Enter the meaning: ");
                    meaning=br.readLine();
                    System.out.println();
                    System.out.print("Enter the usage: ");
                    usage=br.readLine();
                    String x="','";
                    String y="')";
                    query="insert into at(word,meaning,usage) values('"+word.toLowerCase()+x+meaning+x+usage+y;
        //System.out.println("Word is "+word+" Usage is "+usage+" Meaning is "+meaning);
                    try
                    {
                        stmt.execute(query);
                        System.out.println("Suggestion submitted");
                    }
                    catch(Exception aht)
                    {
                        System.out.println("some exception while inserting into admin database");
                    }
                }
               
            }
            s=br.readLine();
            stmt.close();
            con.commit();
            con.close();
            break;
       
/*                                                     case  3                                                     */

        case 3:
            System.out.println("your choice is to login as administrator");
            System.out.print("Enter your adminid: ");
            String id=br.readLine();
            System.out.println();
            System.out.print("Enter your password: ");
            String password=br.readLine();
            System.out.println();
        //System.out.println("Userid is: "+id+" Password is: "+password);
            query="Select * from alt where idd='"+id+"'";
            String p="p";//declared here to overcome scope problems. initialized inside while within try
            try
            {
                rs=stmt.executeQuery(query);
            }
            catch(Exception eeeeee)
            {
                System.out.println("some problem in gettin the result set");
            }
            String i="i";
            if(rs!=null)
            {
                try
                {
                    while(rs.next())
                    {
                        i=rs.getString("id");
                        p=rs.getString("password");
                    }
                }
                catch(Exception eeeeeee)
                {
                    System.out.println("The result set may be null or may be invalid");
                }
                if(i.equals("i"))
                {
                    System.out.println("invalid user id");
                    s=br.readLine();
                    break;
                }
                if(p.equals(password))
                {
                    grant=true;
                    System.out.println();
                    System.out.println("You have logged in successfully!!!");
                    grant=AdminSwitch.admin(true);//function of class having all admin functionalities
                    for(int j=0;j<=80;j++)
                        System.out.println();
                    System.out.println("YOu have logged out successfully");
                    if(grant)
                        grant=false;
       
                }
                else
                {
                    grant=false;       
                    System.out.println("Wrong password");
                    s=br.readLine();
                }
   
            }
   
            break;
        case 4:
            System.out.println("bye!!!");
            s=br.readLine();   
            System.exit(0);
            break;
        }
    }
}
}

 

Database

 

idwordmeaningusage
1Abrupta) Sudden and unexpected.                                                    b)Talking very little and often rude.a)A road with many abrupt turns.                                                        b)When I asked her about her new job she was quite abrupt with me.

 

Source: Thanks to my Students Bhuvaneswari, Deepa and Gayathri,

Comments

Popular Posts