1

I don't understand why Eclipse is giving me an error regarding passing the String returned by the BufferedReader into Hashtable's put(Object,Object) method. I've read the API, I don't recognise any clues. Is it perhaps that it cannot be sure that it will return a unique String?

The String dictionary is set elsewhere in the file,I've stripped this down to the bit that matters - the method in question & anything happening with its variables.

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 } } 
3
  • Including the error you're seeing would be helpful in formulating an answer. Commented Oct 13, 2012 at 19:36
  • seriously you should start using ConcurrentHashMap Commented Oct 13, 2012 at 19:43
  • 1
    Consider: ConcurrentHashMap, Collections.synchronizedMap, try-with-resources, Files.readLines, Files.readAllLines Commented Oct 13, 2012 at 19:57

2 Answers 2

4

It looks like it's not problem with key it is problem with value part. You can not add new Node<E> this as a Value. It must have specific type like new Node<String>() or new Node<Integer>().

Sign up to request clarification or add additional context in comments.

1 Comment

Darn, so simple. Eclipe's error warning message really wasn't helping with this & it was staring me in the face. Thanks.
1

\1. You are not calling the constructor of node: hash.put(br.readLine(), new Node);

To call the default constructor you have to call the "method" with the name of the class like:

new String(); /*or*/ new Node();

\2. The generic type <E> is not seen anywhere in the declaration of the class. To use that you would have to do something like:

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

.. so either go all the way or remove it entirely.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.