2012-10-13 29 views
1

我不明白爲什麼Eclipse給我一個有關將BufferedReader返回的字符串傳入哈希表的put(Object,Object)方法的錯誤。我讀過API,我不認識任何線索。它可能不能確定它會返回一個唯一的字符串嗎?不接受字符串作爲鍵的哈希表

絃樂字典設置該文件中的其他地方,我已經剝離下來到重要的一點 - 有問題&什麼方法,採用它的變量發生。

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Hashtable; 

public class Model { 
    private Hashtable hash=new Hashtable(); 
    private String dictionary; 

    public void loadWords() throws IOException{ 
     BufferedReader br=null; 

     try{   
      br=new BufferedReader(new FileReader(dictionary)); 
      do{ 
       hash.put(br.readLine(), new Node<E>); 
      } 
      while(br.readLine()!=null); 

     }catch(IOException iOE){ 
      System.out.println("Fission mailed"); 
     } 
     finally{ 
      br.close(); // Closing the buffered reader 
     } 
    } 
+0

包括您看到的錯誤將有助於制定答案。 – Simes

+0

認真你應該開始使用[ConcurrentHashMap](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html) –

+1

考慮:ConcurrentHashMap,Collections.synchronizedMap,[嘗試與資源](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html),[Files.readLines](http://docs.guava-libraries.googlecode.com/ git/javadoc/com/google/common/io/Files.html#readLines),[Files.readAllLines](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files .html#readAllLines) – btiernay

回答

4

它看起來像它沒有問題的關鍵它是價值的一部分問題。 您不能將new Node<E>作爲價值添加。它必須具有特定類型,如new Node<String>()new Node<Integer>()

+1

該死,太簡單了。 Eclipe的錯誤警告信息確實沒有幫助這個,它正在盯着我。謝謝。 – gideonparanoid

+2

@Psygnosys你應該接受答案然後... –

1

\ 1。你不調用節點的構造函數: hash.put(br.readLine(),new Node);

要調用默認的構造函數,你必須調用「方法」與類的名稱等:

新的String();/*或*/new Node();

\ 2。在類的聲明中沒有看到通用類型<E>。要使用,你將不得不這樣做:


    public class Model<E> { 
     private Hashtable<String, Node<E>> hash=new Hashtable<String, Node<E>>(); 

..所以要麼一路走,要麼完全刪除它。