Comparison between textPersonName and textPassword [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
My activity it's a simple login activity where (for tests only) it will return true if the username && password are equal and false if not.
But it always return false.
Not even if I convert toString(); example:
String a=(txtUserName.getText().toString() == txtPassword.getText().toString()) ? "equal" : "Nequal";
Full code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtUserName = (EditText) this.findViewById(R.id.txtUname);
txtPassword = (EditText) this.findViewById(R.id.txtPwd);
btnLogin = (Button) this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(LoginActivity.this,txtUserName.getText(),Toast.LENGTH_LONG).show();
Toast.makeText(LoginActivity.this,"Just for separate", Toast.LENGTH_LONG).show();
Toast.makeText(LoginActivity.this,txtPassword.getText(), Toast.LENGTH_LONG).show();
String a=(txtUserName.getText() == txtPassword.getText()) ? "equal" : "Nequal";
Toast.makeText(LoginActivity.this, a, Toast.LENGTH_LONG).show();
}
});
}

Use
String a=(txtUserName.getText().toString().equals(txtPassword.getText().toString()))
The way you are doing are not equating string rather they are equating string objects.
Always use String.equals() method for string comparison.
Java String.equals versus ==

To compare string values, use String's equals method; the == operator for object references compares for reference equality and since they are two different objects it will always return false.
txtUserName.getText().equals(txtPassword.getText())

Related

Query Regarding the below code snippet [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
public static void main(String args[]) throws IOException
{
String s1="abc";
String s2="xyz";
String s3="abcxyz";
String s4=s1.concat(s2);
System.out.println(s4);
#SuppressWarnings("unused")
String s5=s1+s2;
if(s3==s4)
{
System.out.println("true");
}
else
System.out.println("false");
}
Why the o/p is coming as false? According to me it should be true. Can someone please explain
java.lang.String concat() function return new String() object Here s3 and s4 are two different references pointing to two different objects.
== just checks for refernces wheras .equals() checks for the actual content.
s3.equals(s4) will return true.
From javadoc, concat() function.
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
== operator compares String value as well as String memory location. Here, s3 and s4 are using different memory locations.
If you want to compare values of String then you should use s3.equals(s4). It will result into true condition.

Android: Why is my if statement not executing inside the onClick method [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
When I print out the user value and the pass value into LogCat, the values are printed out as "A" and "a". If the user value is "A" and the pass value is "a", I don't understand why the if statement doesn't execute. When I place the contents that are inside the if statement outside of the if statement, the startActivity() method operates correctly. This tells me that the if statement is the problem but I don't see how. I don't get it!
public void onClick(View v)
{
String user = username.getText().toString();
String pass = password.getText().toString();
System.out.println(user);
System.out.println(pass);
if (user == "A" && pass == "a")
{
Intent intent = new Intent("com.example.android.STARTINGPOINT");
startActivity(intent);
}
}
Use the String#equals method to compare strings instead of ==:
if (user.equals("A") && pass.equals("a"))

Can't check string value

I am trying to check a string value in if but it always is entering the else,
what Is wrong here? Thank you
public void alertBtn(View v){
EditText text = (EditText)findViewById(R.id.editText1);
String value = text.getText().toString();
String password="asd";
if (value==password){
new AlertDialog.Builder(this)
.setTitle("Success")
.setMessage("Correct Password")
.setNeutralButton("OK", null)
.show();
}
else
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("Wrong password")
.setNeutralButton("OK", null)
.show();
}
Using the == operator will compare the references to the strings not the string themselves.
String value = text.getText().toString();
String password="asd";
if (value.equals(password))
{
}
use equals() function
Try
if( value.equals(password) ) {
}
Use .equals or .equalsIgnoreCase() to compare strings
What is the difference between == vs equals() in Java?
if (value.equals(password)){
Also move the initialization of editText to onCreate. There is no need to initialize edittext everytime on button click
text = (EditText)findViewById(R.id.editText1); // in onCreate
and declare EditText text as class member
Look here
Use the String.equals(String other) function to compare strings, not the == operator.
The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

trying to detect a string from a pair in a set

For some reason, my == operator isn't returning true when it should. I see two of the exact same strings displayed on my phone yet it's still not registering as true. Any ideas? It's a hashset of pair (string,int). getFirst returns the string in the pair.
private boolean contains(HashSet<Pair> mySet, String current) {
Iterator<Pair> temp = mySet.iterator();
String compared;
Toast.makeText(MainActivity.this, " want " +current,
Toast.LENGTH_LONG).show();
while (temp.hasNext()) {
compared = temp.next().getFirst();
Toast.makeText(MainActivity.this, compared+" "+current,
Toast.LENGTH_SHORT).show();
if (compared==current)
Toast.makeText(MainActivity.this, "found", Toast.LENGTH_SHORT).show();
}
return false;
}
Strings should not be compared with ==. In java, Strings are objects, and == will check if they are the same object reference. If you want to check if they contain the same sequence of characters, use string.equals(otherString).
Do this instead:
if(compared.equals(current))
Use the String.equals() method for comparing strings. As in if (string1.equals(string2))....

why String from intent cant use in "if clause"? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
i have 2 class, let say a.class and b.class
and i want to send array from a to b using intent, in a.class
Intent intent = new Intent(this,b.class);
intent.putExtra("stringtext", "1");
startActivity(intent);
and in b.class i catch the intent value with this
Intent it = getIntent();
String id = it.getStringExtra("stringtext");
when i try to print id, it give me "1"
but when i'm using id in if clause i didnt work, i try this
if(id=="1")
{
teks.setText("its one");
}
else
{
teks.setText("not one";
}
how could this happend?
Use equals() method to compare string, == compares the reference for Object
Make it
if("1".equals(id))
{
teks.setText("its one");
}
See
Java String.equals versus ==
Interview : Java Equals

Categories