Java哈希表中的字典

时间:2014-01-26 18:12:32

标签: java dictionary hashtable

我的程序有问题。我应该创建一个像字典一样的程序。所以我可能做到了,但它不起作用。我有一个菜单,所以它应该添加/编辑/删除文件中的单词。请帮我解决一下我的程序,我想我的代码中都可以理解。问题是它无法在函数中找到文件中的单词

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();
         }
      }


   }
}

1 个答案:

答案 0 :(得分:2)

Hashtable的整个要点(顺便说一下,你应该使用HashMap)是能够通过键找到一个值而不需要迭代。使用其get()方法查找单词的含义,put()插入/更新单词的含义,delete()删除单词含义。

问题在于您使用==来比较字符串,而不是equals(),但如果您曾经使用过映射,则甚至不必比较任何内容,因为地图会为你做比较。