Sean
Sean

Reputation: 51

Dictionary in Java hashtables

I have a trouble with my program. I should to create a program like a dictionary. So I probably did it but it does not work. I have a menu so it should add/edit/delete word in file. please can you help me with my program, i think there are all understandable in my code. the problem is it can not find words in file in function

editWord, deleteWord. and serachTranslation

import java.util.*;
import java.io.*;
public class Dictionary{

   public static Hashtable<String, String> dictionary = new Hashtable<String, String>();

   public static void main(String []args)throws InputMismatchException, IOException{

      BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("dict.in")));
      String line = null;
         while((line = br.readLine())!= null ){
            String [] words = line.split("\\s+");
            String kaz = words[0];
            String eng = words[1];
            Dictionary.dictionary.put(kaz,eng);
         }
      menu();
   }

   public static void menu(){

      //Runtime.getRuntime().exec("clear");

      System.out.println("1 - Add/Edit/Delete word");
      System.out.println("2 - Search translation");
      //System.out.println("3 - Switch language");
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your choice: ");
      int choose  = sc.nextInt();
      switch(choose){
         case 1: secondMenu();
         break;
         case 2: searchTranslation();
         break;
         //case 3: System.out.print("It works 3 case");
         //break;
         default: menu();
         break;
      }
   }

   public static void secondMenu() throws InputMismatchException{
      System.out.println("1 - Add");
      System.out.println("2 - Edit");
      System.out.println("3 - Delete");
      System.out.println("4 - Back");
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your choice: ");
      int choose  = sc.nextInt();
      switch(choose){
         case 1: addWord();
         break;
         case 2: editWord();
         break;
         case 3: deleteWord();
         break;
         case 4: menu();
         break;
         default: secondMenu();
         break;
      }  
   }
   public static void addWord(){
      Scanner w = new Scanner(System.in);
      System.out.println("Enter word in Kazakh language: ");
      String kaz  = w.next();
      System.out.println("Enter word in English language: ");
      String eng = w.next();
      Dictionary.dictionary.put(kaz,eng);
      System.out.println("Sucsess");
      menu();
   }

   public static void editWord(){
      Scanner w = new Scanner(System.in);
      System.out.println("Enter word which you want to change: ");
      String word = w.next();

      Iterator s = Dictionary.dictionary.keySet().iterator();

      while (s.hasNext()) {
      String key = (String) s.next();
      Object value = Dictionary.dictionary.get(key);

         if(word==key){
            System.out.println("Enter new meaning of this word: ");
            String newWord = w.next();
            Dictionary.dictionary.remove(key);
            Dictionary.dictionary.put(word,newWord);
            menu();
         }
         if(word==value){
            System.out.println("Enter new meaning of this word: ");
            String newWord = w.next();
            Dictionary.dictionary.remove(value);
            Dictionary.dictionary.put(newWord,word);
            menu();
         }
      }

   }
   public static void deleteWord(){
      Scanner w = new Scanner(System.in);
      System.out.println("Enter word which you want to delete: ");
      String word = w.next();
      Iterator s = Dictionary.dictionary.keySet().iterator();

      while (s.hasNext()) {
      String key = (String) s.next();
      Object value = Dictionary.dictionary.get(key);

         if(word==key){
            Dictionary.dictionary.remove(key);
            menu();
         }
         if(word==value){
            Dictionary.dictionary.remove(value);
            menu();
         }
      }

   }

   public static void searchTranslation(){
      Scanner w = new Scanner(System.in);
      System.out.println("Enter word which you want to translate: ");
      String word = w.next();
      Iterator s = Dictionary.dictionary.keySet().iterator();
      while (s.hasNext()) {
      String key = (String) s.next();
      Object value = Dictionary.dictionary.get(key);

         if(word==key){
            System.out.println(word+" : "+Dictionary.dictionary.get(value));
            menu();
         }
         if(word==value){
            System.out.println(word+" : "+Dictionary.dictionary.get(key));
            menu();
         }
      }


   }
}

Upvotes: 0

Views: 1400

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The whole point of a Hashtable (you should use HashMap, by the way), is to be able to find a value by key without iterating. Use its get() method to find the meaning of a word, put() to insert/update a meaning of a word, and delete() to delete a word with its meaning.

The problem is that you're using == to compare Strings, instead of equals(), but you shouldn't even have to compare anything if you used to map as it should be used, since the map would do the comparisons for you.

Upvotes: 2

Related Questions