0

Could someone please explain how hashcode works? I am new in android. All I want to do is to get the hashcode of my edittext that the user has inputted.

This is my code:

public class MainActivity extends Activity {
EditText txtCode, txtID;
Button enter;

@Override
        protected void onCreate(Bundle savedInstanceState) 
     super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


     txtID = (EditText) findViewById(R.id.txtid);
     txtCode = (EditText) findViewById(R.id.txtcode);
     enter = (Button) findViewById(R.id.button1);


         enter.setOnClickListener(new OnClickListener() 
            {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub

            String a = txtID.getText().toString();
             int b = a.hashCode();
            txtCode.setText(b);
        }
    });
}
6
  • 1
    hashCode() is not Android-specific. Commented May 29, 2013 at 3:26
  • 1
    What's not working with the code you have now? What error are you getting? Commented May 29, 2013 at 3:29
  • 1
    You should see the javadoc for Object: docs.oracle.com/javase/7/docs/api/java/lang/Object.html -- the .equals()/.hashCode() contract is explained in full. Commented May 29, 2013 at 3:35
  • theres no error actually on my code but theres always fore close on my emulator when i click enter. Commented May 29, 2013 at 3:39
  • how can i get the hashcode of my edittext? Commented May 29, 2013 at 3:45

1 Answer 1

6

Your code is correct. That is how you get the hashcode of some text entered in a text box.

(Or at least, if there is a problem, it is not in that aspect of the code.)

Perhaps you misunderstand the meaning and purpose of hashcode. This method (which is implemented by all Java reference types) returns an int that represents a simple 32-bit hash for the target object:

  • It is not a cryptographic hash. Crypto hashes are generated another way. A hash needs many more than 32 bits before you could even consider it to be "strong".
  • It is not unique. Hashes are never unique.
  • It is not even "probably unique". If you try often enough you are liable to see collisions with hashes for strings. Indeed, since the hash is 32 bits long, you are guaranteed to have seen at least one collision after trying 2^32 different strings. And the mathematics says that you are likely to get a collision much sooner than that.

There's no error actually on my code but there's always force close on my emulator when i click enter.

Use logcat to look for what is causing that "force close". There is likely to be an entry with a stacktrace for the exception that is the immediate cause. If you have trouble figuring out what the stacktrace means, add it to your Question ...

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

Comments

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.