1

I am sorry if this is a silly question but i tried to find the answer but couldn't get. plse help me.. i am new in android studio but have basic java knowledge. Actually, the problem is i am trying to compare value of TextView and EditText like this;

TextView a = (TextView)findViewById(R.id.t1);
EditText b = (EditText)findViewById(R.id.t2);

String x = a.getText().toString();
String y = b.getText().toString();

if(x==y)
{
//----stuff--;
}

Doesn't show any error but the code under 'if' don't execute.

Charuක
  • 12,953
  • 5
  • 50
  • 88
wangz
  • 61
  • 1
  • 10
  • 5
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Vladyslav Matviienko Dec 12 '16 at 11:35
  • 1
    there is nothing special about Android Studio since code won't run in it. It is the same for all IDEs. Mention `Android Studio` only if you face any issues with IDE. – Vladyslav Matviienko Dec 12 '16 at 11:37
  • in your case X == Y will be true if both strings are same. eg "Dog" == "Dog" but case sensitive can return it as false eg "Dog" == "dog" – Charuක Dec 12 '16 at 11:42
  • 1
    @CharukaSilva, only if it’s the same string object. It will be false for two identical string objects (which I suspect the OP is facing). Follow the link to the question that we think this one is a duplicate of for explanation. – Ole V.V. Dec 12 '16 at 11:45
  • @Ole V.V. yes.Good point. so regarding to this question x= a.getText().toString() and y= b.getText().toString() they belongs to same String object or not? just asking to clear things in my head – Charuක Dec 12 '16 at 11:56
  • If the user has entered one of them, that will usually create a new string object separately from the existing one. I f we wanted to be sure, we’d have to dig through a lot of code. Fortunately the right solution is in that other question. – Ole V.V. Dec 12 '16 at 11:59

5 Answers5

1

Use either equals() or equalsIgnoreCase() depending on case sensitivity.

1

Try it like this:

if(x.equalsIgnoreCase(y))
{
//----stuff--;
}

Follow this link to know more about it : http://www.leepoint.net/data/expressions/22compareobjects.html

Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22
1

You can't compare two strings with equal to operator. Use equals or equalsIgnoreCase like below code.

if(x.equalsIgnoreCase(y)){
//return value true if both the strings are same.
}
Maya Mohite
  • 675
  • 6
  • 8
  • 1
    your 'if' code works..but you said 2 strings can't be compare with equal to operator...i could compare 2 strings when i write like this: String x="wangz"; String y="wangz"; if(x==y); it works... – wangz Dec 12 '16 at 12:06
0
String x = a.getText().toString();
String y = b.getText().toString();
boolean correct = x.equals(y);
if(correct)
{
//do stuff
}

use this

Rishabh Mahatha
  • 1,251
  • 9
  • 19
  • thanks for help...but still didn't understand why if(x==y) didn't work... but i am sure to use x.equals(y) now onwards. – wangz Dec 12 '16 at 13:51
  • With == (for strings) in java you are comparing they are of same reference. Like you did is the test if both of them are strings objects. – Rishabh Mahatha Dec 13 '16 at 04:38
0

The == operator tests whether two variables have the same references (identity) aka memory address.

Whereas the equals() method tests whether two variables refer to objects that have the same state (values).

I suggest you to use equals() method if no need to matching case if matching case than use equalsIgnoreCase()

like

if(x.equalsIgnoreCase(y)){
    //both string are equals
 }
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55