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?
 
bobbit31Connect With a Mentor Commented:
public class SomethingOtherThanHashtable
0
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
 
perdoname_Author Commented:
Thank you !
0
All Courses

From novice to tech pro — start learning today.