Hashatable .put [??]

Hello,
I have that piece of code which implements a hashtable and puts the element of an array to it
but while im compiling it  it returns an error "cannot find symbol - method put(java.lang.String, java.lang.String)".
Why's that happening ??? :S

Thanks in advance !


public class Hashtable2
{
    public void makeHashTable2(String [] array2)
    {
                Hashtable hashtable[] = new Hashtable[(array2.length * 2)];

               
               
        for (int i = 0; i< array2.length; i++)
        {
            hashtable.put(array2[i],array2[i]);
        }
    }
}
   
   
perdoname_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobbit31Commented:
Hashtable hashtable = new Hashtable(array2.length * 2);
0
perdoname_Author Commented:
But now it returns another error :
"Cannot find symbol - constructor Hashtable(int)"

???
What's that now ?:?
0
bobbit31Commented:
assuming your the type of your key / object pair is String / String

Hashtable<String,String> hash = new Hashtable<String,String>(2);
    hash.put("Key", "Value");

works for me... what jdk version?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

perdoname_Author Commented:
Do you mean something like that ???
if so it does not work :S:S
I have no idea which jdk version i have :S

import java.util.*;
public class Hashtable
{
    public void makeHashTable(String [] array)
    {
                Hashtable<int, String> hashtable = new Hashtable<int,String>(2);
               //(array.length * 2);

               
               
        for (int i = 0; i< array.length; i++)
        {
            hashtable.put(array[i],array[i]);
        }
    }
}
0
bobbit31Commented:
don't name your class after a reserved word... maybe something like this.

a working example ("javac MyHashtable.java" then "java MyHashtable")

import java.util.*;
public class MyHashtable
{
    public static Hashtable makeHashTable(String [] array)
    {
        Hashtable<String, String> hashtable = new Hashtable<String,String>(array.length * 2);
                               
        for (int i = 0; i < array.length; i++)
        {
            hashtable.put(array[i],array[i]);
        }
        return hashtable;
    }

    // JUST FOR EXAMPLE
    public static void main(String[] args) {
        Hashtable testHash;
       
        testHash = makeHashTable(new String[] { "item 1", "item 2", "item 3" });
       
        System.out.println(testHash.get("item 1"));
        System.out.println(testHash.get("item 2"));
        System.out.println(testHash.get("item 3"));
    }
}
0
bobbit31Commented:
then when you want to remove the example code, you'd be left with something like this:

import java.util.*;
public class MyHashtable
{
    public Hashtable makeHashTable(String [] array)
    {
        Hashtable<String, String> hashtable = new Hashtable<String,String>(array.length * 2);
                               
        for (int i = 0; i < array.length; i++)
        {
            hashtable.put(array[i],array[i]);
        }
        return hashtable;
    }
}
0
perdoname_Author Commented:
Oh thanks very much for the code but now i get another error :S

"type Hashtable does not take parameters"

on that line "        Hashtable<String, String> hashtable = new Hashtable<String,String>(array.length * 2); "

???
0
bobbit31Commented:
please go to your console and type: "java -version" ... what does it say?
0
bobbit31Commented:
p.s. you don't necessarily have to specify a size for your hashtable:

Hashtable<String, String> hashtable = new Hashtable<String,String>();
0
perdoname_Author Commented:
java version 1.5
0
bobbit31Commented:
from the jdk docs:

public Hashtable(int initialCapacity)

Constructs a new, empty hashtable with the specified initial capacity and default load factor, which is 0.75.

Parameters:
initialCapacity - the initial capacity of the hashtable.
Throws:
IllegalArgumentException - if the initial capacity is less than zero.

So, I don't know why you are getting that error... what happens if you remove the parameter (see my prior post)
0
perdoname_Author Commented:
again the same thing :S:S

"type Hashtable does not take parameters"

import java.util.*;
public class Hashtable
{

    public static Hashtable makeHashTable(String [] array)
    {
        Hashtable<String, String> hashtable = new Hashtable<String,String>();
                               
        for (int i = 0; i < array.length; i++)
        {
            hashtable.put(array[i],array[i]);
        }
        return hashtable;
    }
   
}
0
bobbit31Commented:
public class SomethingOtherThanHashtable
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
perdoname_Author Commented:
Thank you !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.