HashMap

Hi there,

I am new to these forums and I am having an error with compiling my code.

This is the code:

Map words = new HashMap();

String word;

words.put(word, new Count(word, 1)); // this is the line that is not compiling

If anyone can offer me any help, that would be great.

Thanks

[330 byte] By [Dougie1610a] at [2007-11-27 11:36:41]
# 1

And the error message is... ?

Have you initialized the variable "word" to have a value?

evnafetsa at 2007-7-29 17:10:16 > top of Java-index,Core,Core APIs...
# 2

Please feel free to share with us your error message. It's just oh so much more productive then having us make wild guesses.

cotton.ma at 2007-7-29 17:10:16 > top of Java-index,Core,Core APIs...
# 3

All local variables should be initialized before Use....

words.put(word,....);

Here word is your key and new Count is your value. In this statement you use the variable word, before intitalizing ....

Java spec, states that all local variables should be initialized before its used..

saracgia at 2007-7-29 17:10:16 > top of Java-index,Core,Core APIs...
# 4

it may be because you are not creating the hashmap with the proper generic types.. for example

/* This map stores Count String pairs where the String is the

key and the Count object is the value stored. */

HashMap< String, Count > map = new HashMap< String, Count > ();

String word;

words.put(word, new Count(word, 1));

smithdale87a at 2007-7-29 17:10:16 > top of Java-index,Core,Core APIs...
# 5

Hi there,

I fixed my problem, it was silly, I made a mistake with the contructor in the Count class.

Sorry to inconvenience anyone.

Thanks

Cheers

Dougie1610a at 2007-7-29 17:10:16 > top of Java-index,Core,Core APIs...