Using Hashtable in JDK 1.2.x

I can't seem to get the Hashtable to work as described in the Sun JDK1.2.2 documentation.
I'm trying:

  private Hashtable users= new Hashtable();
  // initial user database (ahem)
  // users.put("admin", new Integer(1)); // test this because it's identical to Sun example
  users.put("leon", new String("password")); // doesn't work either
  // users.put("leon", "password");                   // My first attempt. Doesn't work either.

I get the compiler error "Type expected"

  users.put("leon", new String("password"));
  and it points to just after the first parenthesis, whichever of the above forms I use.

BTW, I'm writing a servlet (not that that should make any difference, should it?)

Who is Participating?
SmillyConnect With a Mentor Commented:
remove private, if it's in a local funtion....

AstromanAuthor Commented:
Oops. I feel a complete fool (and I've just wasted 30 points).

I'm a total java newbie, and although your answer wasn't strictly correct, it put me in the right direction. The hashtable was private to the class, and then I was trying to add the values to it outside any method - DOH! No wonder it wanted a type...

Anyway, I've moved the population into the init method for now, so thats all sorted.

(mutter mutter, bloody new fangled languages). (Actually, entirely my own stupidity and laziness!).


hehehe glad I could help a little bit......

AstromanAuthor Commented:
If you want a laugh, see how I got frustrated when my code:

if (aString==anotherString){

didn't work! Fortunatly I discoverd toCompare(), which seem to be just like my old favorite, strcmp().

Okay, thats a classic error if you are new with java....

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.