8 Replies - 1716 Views - Last Post: 09 December 2012 - 09:38 AM Rate Topic: -----

#1 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-December 12

Hashtable Java - Problem with .put method

Posted 08 December 2012 - 10:04 PM

So I am trying to make a program to show the use of Hashtable. I ran into a problem when I compiled my program in Java.
I get an error that "cannot find symbol - method put". When I look at other Hashtable programs it is written similar as my code and put method is not declared. I am new to Java so any help would be greatly appreciated. Here is the snippet of code I am having problems with:
Hashtable hTable=new Hashtable();

        hTable.put(new Integer(10), "Ten");
        hTable.put(new Integer(5), "Five");
        hTable.put(new Integer(25), "Twenty-five");
        hTable.put(new Integer(15), "Fifteen");
        hTable.put(new Integer(20), "Twenty");


Is This A Good Question/Topic? 0
  • +

Replies To: Hashtable Java - Problem with .put method

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 12:52 AM

Did you make sure to include the line import java.util.Hashtable; at the top of your java file? Other than that, everything should be working fine. You have done it correctly. Perhaps if you can show us your whole file (assuming it is not too long). Hashtable does have a put method and you can use those integers as keys. Both items are objects so you are ok with putting integer and strings into it.

So perhaps the error is regarding something else and not the code you show there?
Was This Post Helpful? 1
  • +
  • -

#3 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-December 12

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 07:15 AM

I did have the import Hashtable at the top....so here is my code:

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;

public class Hashtable {

    public static void main(String[] args) {

         Hashtable hTable=new Hashtable();

        hTable.put(new Integer(10), "Ten");
        hTable.put(new Integer(5), "Five");
        hTable.put(new Integer(25), "Twenty-five");
        hTable.put(new Integer(15), "Fifteen");
        hTable.put(new Integer(20), "Twenty");

        Set s =hTable.entrySet();

        Iterator i=s.iterator();

        while(i.hasNext())
        {
            Map.Entry m=(Map.Entry)i.next();

            int key = (Integer)m.getKey();
            String value=(String)m.getValue();

            System.out.println("Key :"+key+"  value :"+value);
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 07:49 AM

You name your class the same as the existing Hashtable class ^^
Rename your class :P

Or else you will have to declare your Hashtable like this
java.util.Hashtable hTable = new java.util.Hashtable();


This post has been edited by CasiOo: 09 December 2012 - 07:49 AM

Was This Post Helpful? 1
  • +
  • -

#5 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-December 12

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 08:21 AM

Oh that makes sense thank you! I tried to compile it now with the changed class name but get a note from Java "Recompile with -Xlint:unchecked for details. ?
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 09:01 AM

View Postabbey21, on 09 December 2012 - 03:21 PM, said:

Oh that makes sense thank you! I tried to compile it now with the changed class name but get a note from Java "Recompile with -Xlint:unchecked for details. ?


Uhm I don't know what that is supposed to mean

Did you remember to change the name of the file as well?
Was This Post Helpful? 1
  • +
  • -

#7 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-December 12

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 09:23 AM

Yes I did it says:
Note: HashtableExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint: unchecked for details

public class HashtableExample {

    public static void main(String[] args) {

	//Creating object if Hashtable
         Hashtable hTable=new Hashtable();

	//
        hTable.put(new Integer(10), "Ten");
        hTable.put(new Integer(5), "Five");
        hTable.put(new Integer(25), "Twenty-five");
        hTable.put(new Integer(15), "Fifteen");
        hTable.put(new Integer(20), "Twenty");

        Set s =hTable.entrySet();

        Iterator i=s.iterator();

        while(i.hasNext())
        {
            Map.Entry m=(Map.Entry)i.next();

            int key = (Integer)m.getKey();
            String value=(String)m.getValue();

            System.out.println("Key :"+key+"  value :"+value);
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 09:30 AM

Eclispe gives some very good messages of what you might be doing wrong

You are getting some warnings because a Hashtable is generic, that is you need to tell it the types you are gonna use with it
It is only warnings and can be ignored, the program should still be able to run

Your example runs without any problems here
I ran the following code
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashtableExample {

    public static void main(String[] args) {

	//Creating object if Hashtable
         Hashtable hTable=new Hashtable();

	//
        hTable.put(new Integer(10), "Ten");
        hTable.put(new Integer(5), "Five");
        hTable.put(new Integer(25), "Twenty-five");
        hTable.put(new Integer(15), "Fifteen");
        hTable.put(new Integer(20), "Twenty");

        Set s =hTable.entrySet();

        Iterator i=s.iterator();

        while(i.hasNext())
        {
            Map.Entry m=(Map.Entry)i.next();

            int key = (Integer)m.getKey();
            String value=(String)m.getValue();

            System.out.println("Key :"+key+"  value :"+value);
        }
    }
}




If you want to add generics for type safety (you really should), then you could change it to
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class HashtableExample {

	public static void main(String[] args) {
		//Creating object if Hashtable
		Hashtable<Integer, String> hTable = new Hashtable<Integer, String>();

		hTable.put(new Integer(10), "Ten");
		hTable.put(new Integer(5), "Five");
		hTable.put(new Integer(25), "Twenty-five");
		hTable.put(new Integer(15), "Fifteen");
		hTable.put(new Integer(20), "Twenty");

		Set<Entry<Integer, String>> s = hTable.entrySet();

		Iterator<Entry<Integer, String>> i = s.iterator();

		while (i.hasNext()) {
			Entry<Integer, String> m = i.next();

			int key = m.getKey();
			String value = m.getValue();

			System.out.println("Key :" + key + "  value :" + value);
		}
	}
}




It is not normal to go through every entry in a Hashtable, that is not what it is made for, but I guess it can be needed sometimes

This post has been edited by CasiOo: 09 December 2012 - 09:31 AM

Was This Post Helpful? 1
  • +
  • -

#9 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-December 12

Re: Hashtable Java - Problem with .put method

Posted 09 December 2012 - 09:38 AM

It works now with the generics in the code. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1