HashCode, trying to get the last couple letters of a word or number
I have this hashCode function that I have been trying to implement with Eclipse but my T开发者_Python百科ests 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
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 :)
It may be a problem with the editor mangling the code, but I do not see value
or test
used anywhere inside the loop.
精彩评论