2012-10-13 34 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>>(); 

..所以要么一路走,要么完全删除它。