Comparing String to String is not working [duplicate] - android

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have this code and to me it seems like the logic is correct. When the value.toString() is entered it prints out 0123456789 just like it should because that is the value I entered in the editText field. Is there something simple that I am missing here or is this fine and I will have to look further afield.
Editable value = input.getText();
Log.i("Password entered: ", value.toString());
if(value.toString() == "0123456789"){
Log.i("Password entered: ", "yay it is working!");
} else {
Log.i("Password entered: ", "it is incorrect");
}

Use .equals to compare strings.
if(value.toString().equals("0123456789"))

== tests for reference equality.
.equals() tests for value equality.
value.toString().equals("0123456789")
will work as intended.

You can compare Your String with Following Code
value.toString().equalsIgnoreCase("0123456789");
It gives you best result.

Try this:
((value.toString()).compareTo("0123456789") == 0){
//Do somthing here
}

Related

The string contain "/" can't be compared in android? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I think Method A will displays "Ok", but in fact it displays "Fails". The Method B can get the correct result "OK".
I'm sure that the function fi.iki.elonen.NanoHTTPD.getMimeTypeForFile("my.css") will return the result "text/css".
I don't understand why the Method A can't get correct result. Is there some bugs with the function fi.iki.elonen.NanoHTTPD.getMimeTypeForFile ?
BTW, Method C can get the correct result "OK".
Method A
String a="text/css";
String b= fi.iki.elonen.NanoHTTPD.getMimeTypeForFile("my.css");
Utility.LogError("B: "+b);
if (a==b){
Utility.LogError("Ok");
}else{
Utility.LogError("Fails");
}
Method B
String a="text/css";
String b= fi.iki.elonen.NanoHTTPD.getMimeTypeForFile("my.css");
Utility.LogError("B: "+b);
if (a.compareTo(b)==0){
Utility.LogError("Ok");
}else{
Utility.LogError("Fails");
}
Method C
String a="text/css";
String b= "text/css";
Utility.LogError("B: "+b);
if (a==b){
Utility.LogError("Ok");
}else{
Utility.LogError("Fails");
}
Method 1
It results to "Fails"
It because the actual objects on heap are getting compared when you use ==
reference : Detailed explanation
Method 2
It results in Ok as a and b contain same text(mime type) in them (using compare to)
Method 3
It results in Ok, as expected.
In method A , you judge "a==b" which means a have the same reference as b.Obviously their reference is different.
Beacause , In the case A, "if(a == b)",a is a memory address ,as the same, b is a memory address ,ofcourse they are not the same !
and In the case B,you compare to a & b 's value! so they are the same .
To compare strings in Java you must use equals().
String a = "abc";
String b = "abc";
if(a.equals(b)) {
// true!
}
When you use ==, java is comparing objects references, not its value.

What's wrong with this IF...AND statement? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have the code below in an Android method. When I debug the code the value of callType is "upload_latest" and the value of uploaded_date (defined as long) is 1424286105554, but code execution bypasses the "return false" statement and goes straight to the string declaration below it. I know I've missed something obvious but can't work out what it is - can anyone help?
if (callType == "upload_latest" && uploaded_date > 0) {
return false;
}
String extension = "";
If callType is of type String (or any other non-primitive type), you need to use the .equals() method rather than the == operator. See this question for more information.
To compare String values use .equals, == tests for reference equality.
callType.equals("upload_latest")
Try something like this:
if ((callType.equals("upload_latest"))&& (uploaded_date > 0))) {
return false;
}
String extension = "";
use callType.equals("upload_latest") instead of callType == "upload_latest"
The == operator checks to see of the strings are the same instance where .equals() checks to see if they represent the same character sequence.

Use Edit Text for password change

I'm new android developer and i want to check is two Edit Text match with each other or not ,I want it for password change.
here is my code [edited] :
String ChPassword1=ChangePassword1_Box.getText().toString();
String ChPassword2=ChangePassword2_Box.getText().toString();
if(ChPassword1==ChPassword2){
savePreferences("PASSWORD", ChPassword1);
Toast msg = Toast.makeText(getBaseContext(),"رمز تغییر کرد", Toast.LENGTH_LONG);
msg.show();
}
but it doesn't work !!
use the equals method from String to compare strings
if (ChPassword1.equals(ChPassword2)) {
}
== compares strings reference
Better use
ChPassword1.equals(ChPassword2)
Use equals for String matching
if(ChPassword1.equals(ChPassword2)

Only else part is executed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
even whel length is 0 and flag9 is null only else part is executed.can someone help me
Log.d(TAG, "flag8 "+ getIntent().getExtras().getString("flag").length());
Log.d(TAG, "flag9 "+ getIntent().getExtras().getString("flag"));
if( getIntent().getExtras().getString("flag")=="0" ||getIntent().getExtras().getString("flag").length()==0)
{
Log.d(TAG, "hidebeta "+ getIntent().getExtras().getString("flag"));
beta.putExtra("beta", "hidebeta");
flag=1;
beta.putExtra("flag", "1");
}
else {
beta.putExtra("beta","showbeta");
Log.d(TAG, "showbeta "+ getIntent().getExtras().getString("flag"));
flag=0;
beta.putExtra("flag","0");
}
In Java you cannot use == to compare Strings, you must use:
if(string.equals("example"))
So let's use equals() in your conditional and optimize it:
String flag = getIntent().getStringExtra("flag");
if(flag.equals("0") || flag.length()==0)
(Also you ought to be safe and check if getIntent() and flag are not null before trying to access them.)
Read How do I compare strings in Java? or the documentation on Comparing Strings for more information.
use equals to compare strings, not == (which compares references to objects)
You should never use == operator for comparing two Strings, as it compares the actual references, not their values. Use equals() method instead. Hope this helps.

Compare two things doesn't work

I have a strange problem in my android app. I must compare two string which are equals. I tried this :
if (raspunsdata.equals(rok)) {
System.out.println("changed ");
} else
System.out.println("no change");
}
but I get always "no change". Before this I have System.out.println for both strings, and both of them have the same value.
I tried also (raspunsdata==rok) and raspunsdata.contentEquals(rok) but I have the same problem. Why? I cant understand this.,...please help...
You might have unwanted white spaces. Might need to use the trim function just to make sure.
if (raspunsdata.trim.equals(rok.trim())) {
System.out.println("changed ");
} else
System.out.println("no change");
}
Btw equals is the correct way to check whether the values are the same.
.equals - compares the values of both objects. If you have 2 Strings with the same characters sets .equals will return true;
== - compares if two objects references are equal.
For example:
String a = "lol";
String b = a;
a == b - will be true.
Try reading: http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml

Categories

Resources