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.

Page 2 of 2 FirstFirst 12
Results 26 to 29 of 29

Thread: Dictionary using Distributed Hash Table and BST Tree

  1. #26
    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: Dictionary using Distributed Hash Table and BST Tree

    How/where did you get the algorithm for the code in the hash() method?

    What are the meanings for the constants: 53 and 96?

    It should give me numbers 1-7 and for "g" it gives me 0.
    Execute the method with single letters from a to z and see what is returned. The pattern should give you some idea how to fix it.
    Last edited by Norm; June 23rd, 2012 at 11:24 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dictionary using Distributed Hash Table and BST Tree

    So: 1. I got it from a friend.
    2. constant 53 is: number of small letters + big letters + space.
    3. 96 is the code of "`" before "a" in the ASCII code.
    4. l is the size of the dictionary.

    I noticed it is only good for the lower case letters.
    corrected:
    public static int hash(String key, int l)
        {
            int value = 0;
     
            for(int j = 0; j < key.length(); j++ ) 
            {
                value = (value * 53 + toDig(key.charAt(j))); 
            }
     
            value = value % l;
     
            return value;
        }
     
        private static int toDig(char a)
        {
            int hMany = 0;
            if(Character.isLowerCase(a))
            {
                hMany = a - ('a' - 1);
            } else if(Character.isUpperCase(a)) {
                hMany = a - ('A' - 27);
            } else if(a == ' '){
                hMany = 53;
            }
            return hMany;
        }

    //It works now.
    Last edited by dezett; June 23rd, 2012 at 12:02 PM.

  3. #28
    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: Dictionary using Distributed Hash Table and BST Tree

    When posting code please wrap your code with
    [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.

  4. #29
    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: Dictionary using Distributed Hash Table and BST Tree

    There is a substring() method call on line 39 where the value of the index is out of bounds/past the end of the String.
    Look at the code on that line and see why: how long is the String & what is the value of the index.
    Use a println to print out those values if you need to see what they are.

    One problem with the code is the last else does NOT test the first letter of the function String. It should test for that and there should be an ending else clause that prints an error message if none of the above test were true.
    Last edited by Norm; June 23rd, 2012 at 12:08 PM. Reason: missing test and else
    If you don't understand my answer, don't ignore it, ask a question.

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

    dezett (October 18th, 2012)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. hash table probes
    By victorh in forum Collections and Generics
    Replies: 4
    Last Post: November 8th, 2011, 12:33 AM
  2. Unscrambler, trying to get the last word of a dictionary...
    By csharp100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 03:44 AM
  3. [Jade-Java]Distributed Container
    By ermasto in forum Algorithms & Recursion
    Replies: 0
    Last Post: January 25th, 2011, 06:08 AM
  4. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  5. Find the password with dictionary attack
    By mortis1572 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 19th, 2010, 10:07 PM

Tags for this Thread