-1

I am trying to toast a string that is pulling from a password EditText but my result is coming up blank.

I am guessing that this is because the edit text is a password type of field and it is encoded somehow but I don't know how else to get around this.

Obviously I don't want to toast the password in the live app, this is for testing purposes, but even in my non-toast test, it still is not working.

Here is my simple code:

public void SubmitRegistration(View view) {

           // assign text in fields to string values
           EditText first = (EditText)findViewById(R.id.first);
           String first2 = first.getText().toString();

           EditText last = (EditText)findViewById(R.id.last);
           String last2 = last.getText().toString();

           EditText display = (EditText)findViewById(R.id.display);
           String display2 = display.getText().toString();

           EditText email = (EditText)findViewById(R.id.email);
           String email2 = email.getText().toString();

           EditText password = (EditText)findViewById(R.id.password);
           String password2 = password.getText().toString();

           EditText vpassword = (EditText)findViewById(R.id.vpassword);
           String vpassword2 = vpassword.getText().toString();   

           Toast.makeText(getApplicationContext(), "password 1 = " + password2, Toast.LENGTH_SHORT).show();
           // if passwords match, send php url

           if(vpassword2 == password2)
           {
               new RequestTask().execute("http://test");       
           }
           else { 
               Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_SHORT).show(); 
           }        
   }
user3053446
  • 124
  • 2
  • 13
  • 1
    Where exactly is the code located? In an `onClick()` handler? Note you're getting the contents from `first` twice and comparing strings with `==` which most of the time won't work (use `equals()`). – laalto Dec 29 '13 at 08:46
  • Ok, now after the edit the code makes more sense. There's still the string comparison problem, making two toasts to show on top of each other. If `password2` comes out as blank, make sure the `EditText` you're entering it into is in fact the same edittext as referenced by `id.password`. – laalto Dec 29 '13 at 08:56
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – laalto Dec 29 '13 at 09:04

1 Answers1

3

First, you have shown us two EditTexts referenced by password ans vpassword identifiers but getting the password text from a different EditText referenced by first identifier...

may be you want to do like this...

       EditText password = (EditText)findViewById(R.id.password);
       String password2 = password.getText().toString();

       EditText vpassword = (EditText)findViewById(R.id.vpassword);
       String vpassword2 = vpassword.getText().toString();

second, don't use == operator when comparing Strings. use equals() method...

 vpassword2.equals(password2);
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • I accidentally put the wrong edittext name, ignore that. But your equals() worked just fine, all fixed, thank you! But do you know why "==" doesn't work? – user3053446 Dec 29 '13 at 08:52
  • 1
    == used for reference comparison where as equals() method used for verifying the equivalence of contents of a String... for more info, see the thread here http://stackoverflow.com/questions/3885753/string-comparison-and-string-interning-in-java – Gopal Gopi Dec 29 '13 at 08:55