I have to create a hash table using linear probing that will allow to search the state capitals of the united states. If you could, please look at my code and see where I am going wrong!
import java.io.*;
import java.util.Scanner;
public class CapitalsTable {
private CapitalsHash[] capitalArray;
private int arraySize;
private CapitalsHash nonCapital;
public CapitalsTable(int size)
{
arraySize=size;
capitalArray = new CapitalsHash[arraySize];
nonCapital = new CapitalsHash(-1);
}
public void displayTable()
{
System.out.print("Table:");
for (int j=0;j<arraySize;j++)
{
if(capitalArray[j] !=null)
System.out.print(capitalArray[j].getKey() + " ");
else
System.out.print("**");
}
System.out.println("");
}
public static int hashFunc(String key)
{
int hashVal=0;
for(int j=0;j<key.length();j++)
{
hashVal=(127*hashVal+key.charAt(j))
%75;
}
return hashVal;
}
public void insert(CapitalsHash state)
{
int key = state.getKey();
int hashVal = hashFunc(key);
while(capitalArray[hashVal] !=null &&
capitalArray[hashVal].getKey() !=-1)
{
++hashVal;
hashVal %=arraySize;
}
capitalArray[hashVal] = state;
}
public CapitalsHash delete(int key)
{
int hashVal = hashFunc(key);
while (capitalArray[hashVal] !=null)
{
if(capitalArray[hashVal].getKey() == key)
{
CapitalsHash temp = capitalArray[hashVal];
capitalArray[hashVal] = nonCapital;
return temp;
}
++hashVal;
hashVal %=arraySize;
}
return null;
}
public CapitalsHash find(int key)
{
int hashVal = hashFunc(key);
while (capitalArray[hashVal] !=null)
{
if(capitalArray[hashVal].getKey() == key)
return capitalArray[hashVal];
++hashVal;
hashVal %=arraySize;
}
return null;
}
}
In my insert, delete, and find functions, netbeans keeps telling me that "key" is not an integer. please help!


Sign In
Create Account

Back to top









