Results 1 to 4 of 4
  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default hashCode questions

    I have this hashCode function that I have been trying to implement with Eclipse but my Tests keep failing. I am trying to write a Hashcode function that returns that last 2 or 3 chars of a word or number. Here is my current code:

    Java Code:
    public int hashCode(String value){
     String test = value;
     int hash = 1;
     int len = test.length();
     System.out.println(len);
     for ( int i=0; i<len; i++ )
           {
           hash = 31 * hash + len;
           }
        return hash;
    }
    Here is my Junit Test:

    Java Code:
    @Test // hashCode()
     public void testHashCode1() {
      //Integer key1 = 123;
      String key2 = "and";
      assertEquals("and", key2.hashCode());
     }
    Everytime I run my test it fails indicating:

    expected but was: <96727>

    Now I really would like for this function to take in either a Word or Number and return the last two or three chars of the object taken in. Does anyone know how to do this using hashCode. Any help would be most appreciated

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As you've already been told here.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by masijade View Post
    As you've already been told here.
    Please read regarding cross-posting: BeForthrightWhenCrossPostingToOtherSites

  4. #4
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

Similar Threads

  1. An acceptable hashcode?
    By dsym@comcast.net in forum New To Java
    Replies: 7
    Last Post: 03-29-2009, 09:04 PM
  2. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 01:13 AM
  3. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM
  4. Why Equals method should be over ridden in Hashcode?
    By skyineyes in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 04:13 PM
  5. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •