2

I've been stuck at this problem for a few hours now. I've commented out all code thinking it was something to do with array out of bounds but this is still happening. I'm trying to read input from a file using scanner, storing data and getting that data later using the hashcode. But the hashed value keeps changing.

public static void main(String[] args)
{
    //only prior code is to access data
    char arr[] = new char[25];
    arr = readString.toCharArray();
    int y;
    y = hash(arr);
}

public static int hash(char[] arr)
{
    int get = arr.toString().hashCode();
    System.out.println(y);
    return get; 
}

for the file, even with same thing on every line, there is still this problem
ex.
hello
hello
hello

i've commented out all other functions except to get the data and hash it but not sure why hashcode has differnt values. Am I using hashcode incorrectly? Is there another way to do this?

Edit* When I hash strings, inside main funcitons, value is always the same, but values that I retrieve have never been the same.

2
  • What's key? I don't see it defined or set, but you're using it in the first line of hash() Commented Apr 30, 2012 at 19:37
  • Why do you create an array then remove all references to it? Commented Apr 30, 2012 at 19:39

3 Answers 3

7

The problem is this line:

   int get = arr.toString().hashCode();

You're expecting arr.toString() to return the string in the char[]. It doesn't.

From Object.toString():

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())

So arr.toString() returns something like "[C@3e25a5", whose value will change each time, and whose hashCode() will consequently also change.

Sign up to request clarification or add additional context in comments.

4 Comments

when I have String temp = arr.toString(); get = temp.hashCode(); still has same error though.
Yes, but look at the value of temp each time. :)
thank you so much, I have used String temp = new String(record), I had code exactly like this earlier, but fed above to hashcode. It works now. Thanks.
You're getting Object.toString(), which will return something like [C@address, where [C means array of characters. You could use java.util.Arrays.toString(array), or java.util.hashCode(array).
1

You must use Arrays.toString(arr) instead of arr.toString()

http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#toString(char[])

Comments

0

Yes this is going to print out the same thing every time. Your method is printing the hashCode of key, which is not a function parameter. So presumably you've defined this somewhere in code you haven't shown us... why should its hashCode change?

1 Comment

He doesn't expect it to change, but apparently it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.