Creating my own Hashtable methods
Hello,
I have a bit of problem. I am trying to insert strings into a hashtable. I finally got my program to compile so I wanted to check if my insert method works. I've tried different approaches and hit a brick wall. Can someone please help? The reason I'm creating my own methods instead of using java.util.Hashtable is I was instructed to implement my own. Here is my first code (before trying to write items in the hashtable):
public class HashtableReserved
{
//this is the hashfunction that will be used to insert the reserved (strings)
//words into the table
//it will take and up the ASCII values oand distrubite
//into the table
public static int hash ( String key)
{
int hashVal = 0;
int tableSize = 101;//the size of the hashtable
for (int i = 0; i <key.length(); i++)
hashVal += key.charAt(i);
return hashVal % tableSize;
}
//This is the insert method for the hashtable
//it will insert the string in this case the reserved word
//and insert the string into the hashtable
//according to the hash function above
public void insert (String key)
{
htr.hash(key);
if (htr != null)
htr.insert("throw");
}
Then I made the following changes to my code:
public class HashtableReserved
{
private static HashtableReserved htr = null;//changed
//this is the hashfunction that will be used to insert the reserved (strings)
//words into the table
//it will take and up the ASCII values oand distrubite into the table
//This code is taken from figure 5.2
public static int hash ( String key)
{
int hashVal = 0;
int tableSize = 101;//the size of the hashtable
for (int i = 0; i ><key.length(); i++)
hashVal += key.charAt(i);
return hashVal % tableSize;
}
//This is the insert method for the hashtable
//it will insert the string in this case the reserved word
//and insert the string into the hashtable
//according to the hash function above
public static HashtableReserved insert()
{
htr.hash();
if (htr != null)
htr.insert("throw");
return htr;
}//end of new insert
public static String find (String key)//new method
{
String word = htr.get(key).toString();
return word;
}//end of method
//main program of the hashtable
//checking to see if insert method works
public static void main (String [] args)
{
insert();
find("throw");
System.out.println(find("throw"));;
}
}
So, now I'm having the following compiler errors:
method hash()not found in class HashtableReserved
method insert(java.lang.string) and method get(java.lang.String) not found in class HashtableReserved
Can anyone see where I am going wrong? Or tell if my first insert method is working?
>

