Hayrullah Cansu
Hayrullah Cansu

Reputation: 262

Android equalsIgnoreCase not working correctly

temp1 is "accountant"editText is "accountant"

but i checked with String.length();

I tried trim() method, i got the same result again.

Code sample:

    if(temp1.equalsIgnoreCase(editText1.getText().toString().trim()))
      {
        //do someting
      }

I solved.

Upvotes: 0

Views: 1152

Answers (2)

RichFounders
RichFounders

Reputation: 169

You may try this code :

    //put this below public class activity

    int index=0; 
    String [] answer ={"Accountant"};

and put this button code below setContentView

buttonanswer = (Button)findViewById(R.id.yourbutton);
buttonanswer.setOnClickListener(this);

and then put this code

    @Override
    public void onClick(View v) {               

    edittextanswer=(EditText)findViewById(R.id.youreditText); 
    if(v==buttonanswer) {

        String myanswer=edittextanswer.getText().toString();              
        if(myanswer.equalsIgnoreCase(answer[index]))
        {
            //do someting
        }
        else
        {    
           //do someting
        }

Upvotes: 0

Henry98
Henry98

Reputation: 571

Since temp1 is a string and has 11 characters, you must have placed a whitespace after the word accountant. (Either that or you flat out misspelled the word.) Accountant is only 10 characters long, so there must be whitespace. (Well, it's pretty guaranteed since you, yourself, created it.)

Either remove the whitespace from temp1, or trim temp1 before you compare. Also, double-check temp1 to actually make sure you spelled the word correctly.

Upvotes: 2

Related Questions