Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 611783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:49:18+00:00 2026-05-13T17:49:18+00:00

I have this hashCode function that I have been trying to implement with Eclipse

  • 0

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:

 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:

    @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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:49:18+00:00Added an answer on May 13, 2026 at 5:49 pm

    public int hashCode(String value)

    and calling as

    assertEquals(“and”, key2.hashCode());

    you are not even overriding it properly change the signature to take no arguments please 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that generates a MD5 hash in C# like this: MD5
We are told that we should implement hashCode() for our classes but most people
I remember eclipse and idea have this template to automatically create an object's hashCode
I have a DLL in which I have a function that returns a pchar.
I have a class called Node that I've written. I overrode the hashCode() function
In relation to this question ( Efficient hashCode() implementation ) I have one more
I have this application where I implement the ActionBar Fragment interface. Underlying the interface,
I have this function // add history paths and save data function AddPath( strTag,
I have this VM with tomcat, java, and grails in it. I've been getting
I have a vector class with hashCode() implemented. It wasn't written by me, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.