Jump to content

Hash Tables with Linear Probing

- - - - -

  • Please log in to reply
9 replies to this topic

#1
TCooney

TCooney

    Newbie

  • Members
  • Pip
  • 5 posts
Hi There, I am a college student and need some help with an assignment I am working on.

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!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
in HashFunc, you passed key as a string.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
TCooney

TCooney

    Newbie

  • Members
  • Pip
  • 5 posts
Because the Key is a string, I am inputing a state name

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Which line is it complaining about?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Xylyze

Xylyze

    Newbie

  • Members
  • Pip
  • 8 posts
Can you show the code for the class CapitalsHash?

Also, at line 42:
int hashVal = hashFunc(key);
You pass an int but hashFunc expects a String.

#6
TCooney

TCooney

    Newbie

  • Members
  • Pip
  • 5 posts
public class CapitalsHash {
    private int Capitals;

    public CapitalsHash(int ii)
    {
        Capitals=ii;
    }
    public int getKey()
    {
        return Capitals;
    }
}

It's complaining about
int hashVal = hashFunc(key);


#7
TCooney

TCooney

    Newbie

  • Members
  • Pip
  • 5 posts
If you guys think I'm going the wrong way about, please feel free to let me know what you think might work better

#8
Xylyze

Xylyze

    Newbie

  • Members
  • Pip
  • 8 posts
If you use:
int hashVal = hashFunc(key+"");
instead of:
int hashVal = hashFunc(key);
your code will compile atleast. But I'm not sure if it's going to do what you want it to do. (if that made any sense)

#9
Xylyze

Xylyze

    Newbie

  • Members
  • Pip
  • 8 posts

TCooney said:

If you guys think I'm going the wrong way about, please feel free to let me know what you think might work better

Well for starters, the CapitalsHash class is a wrapper class for just one int. In the constructor you set the value and with the getKey method you get the value. So in my opinion the whole class is redundant. You can always use Integer if you don't want to work with the primitive int.

#10
TCooney

TCooney

    Newbie

  • Members
  • Pip
  • 5 posts
Well it did take away the errors, I'll see if I can get it to work now and let you guys know




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users