Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: PLEASE HELP! (hashtable in Java)

  1. #1
    Junior Member hedyeh's Avatar
    Join Date
    Apr 2013
    Location
    Northern Virginia
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default PLEASE HELP! (hashtable in Java)

    Dear all,

    I have a problem with my code. There are some bugs which I don't know how to fix!

    The code is supposed to use the division method of hashing to store the data values into hash tables with different table sizes, and be able to search for a specific number. The linear probing is used to minimize the collisions. I used the methods provided in my course textbook (hash, add and get). So the methods are correct.

    The compiler cannot compile my code and gives for the line:

    tablesize7.add((Hashable)start);

    the following error:

    inconvertible types.
    found: int; required: Hashtable.hashable

    but I cannot define variable start as a Hashable. Right? Because when I do that, then the compiler cannot calculate the following:

    return(element%max_elements);


    Here's the code:

    package Hashtable;
     
     
    public interface Hashable
    {
         int hash();
    }


    package Hashtable;
     
    public class HashDataset implements Hashable {
     
       static int element; 
       static Hashable list[];
       static int max_elements;   
       protected int numElements = 0;
       static int tableSize;
       static int start;
     
       public int hash() { 
          return(element%max_elements);
       }
     
       public void add (Hashable element)  {
     
         int location = element.hash();
     
          while(list[location]!=null) {
              location = (location + 1) % list.length;
              list[location]=element;
              numElements++; 
      } 
    } 
     
      public Hashable get(Hashable element) {
     
         int location = element.hash();
     
        while(list[location]!=element)
           location = (location + 1) % list.length;
     
        return (Hashable)list[location];
    }
     
     
     
    public HashDataset(int start, Hashable list[], int tableSize)  {
     
          element=start;
          this.list = list;
          max_elements = tableSize;
     
      }
     
    public static void main(String[] args) {
     
       HashDataset tablesize7 = new HashDataset(1500, new Hashable[150], 7); 
     
       for(int i=0; i<100; i++)  {  
         tablesize7.hash();
         tablesize7.add((Hashable)start);
         System.out.println(start);
     
     
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: PLEASE HELP! (hashtable in Java)

    But the program doesn't work!
    Please explain what the code does and what the problem is.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    hedyeh (April 11th, 2013)

  4. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: PLEASE HELP! (hashtable in Java)

    The problem is on line tablesize7.add((Hashable)start); "start" is an int so it cannot be cast to "Hashable".

    --- Update ---

    It seems to me like you're using your interface incorrectly. Your dataset isnt what is "hashable", the data should be. If this is what you're trying to do, maybe you should be making a class called HashableInt and have that implement "Hashable".
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. hashtable dilema
    By Javanoob12 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2012, 03:29 PM
  2. [SOLVED] Question about hashtable
    By leonne in forum Java Theory & Questions
    Replies: 3
    Last Post: November 4th, 2012, 12:09 PM
  3. I need help with Enumeration in hashtable
    By jaouharia1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2012, 08:17 PM
  4. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM

Tags for this Thread