This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have a simple piece of code to validate a username and a password.
public boolean isValid(String u, String p) {
if (u=="admin" && p=="password1") {
return true;
} else if (u=="user" && p=="password2") {
return true;
} else {
return false;
}
}
I've tried debugging it, and when it runs, u has the value "admin" and p has the value "password1", but it just skips the first condition. I must have done something wrong, but I can't figure out what.
== should not be used for String comparison. Use equals() instead.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
i know that my question is a duplicate of some question here before, i tried every solution that i see from that, but no solution work for me
i have a class named FilingModel and a method name getReason, i always get a null value to my tvReason TextView
public class FilingAdapter extends RecyclerView.Adapter<FilingAdapter.FilingHolder> {
List<FilingModel> lists;
#Override
public void onBindViewHolder(#NonNull FilingHolder holder, int position) {
FilingModel model = lists.get(position);
holder.tvReason.setText(model.getReason());
}
}
public class FilingModel {
private String reason;
public FilingModel(String reason) {
this.reason = reason;
}
public String getReason() {
if ( reason.isEmpty() || TextUtils.isEmpty(reason) || reason.equals(null)) {
reason = "-";
}
return reason;
}
}
String checks for null are rather straight forward (once you have done it):
First you want to check if the instance is null (otherwise all further manipulation might fail):
if(reason == null)
Then you want to check if it is empty as you already did it but maybe you want to trim it first (to remove all whitespaces):
if(reason == null || reason.trim().isEmpty())
as you can see you can chain the conditions with or since Java will stop evaluating the conditions once one of them was found true . So if your string is null, Java will not evaluate if it is empty.
And that's all you need, null and isEmpty (and optionally with a trim())
public String getReason() {
if(reason==null || reason.trim().isEmpty()){
reason = "-";
}
return reason;
}
Give type to your variable reason. For example String.
private String reason;
+
if(reason == null)
This question already has answers here:
Comparing boxed Long values 127 and 128
(4 answers)
What's the difference between ".equals" and "=="? [duplicate]
(11 answers)
Closed 7 years ago.
How to compare two long value at runtime. When I got the value of both of long type variable at runtime which same so it should be print else part but the both value is different from each other so it should be print if part.
Long dbData = 54188439.... // Got the value at run time
Long spreadSheet = 54188439.....//Got the value at run time
if(dbData != spreadSheet)
{
Log.i("","Please update your contact");
}
else
{
Log.i("","Not required");
}
Here I always got if part whatever be the condition. Please help me out.
The reason it won't work is because you are comparing the objects, not the values. See also https://stackoverflow.com/a/8968390/4890300 for a great answer.
Make use of equals operator
changed the statement from
if(dbData != spreadSheet)
to
if(!dbData.equals(spreadSheet))
When Same
public static void main(String[] args) {
Long dbData = new Long(54188439);
Long spreadSheet = new Long(54188439);
if (!dbData.equals(spreadSheet)) {
System.out.println("Please update your contact");
} else {
System.out.println("Not required");
}
}
Output
Not required
When Different
public static void main(String[] args) {
Long dbData = new Long(54188439);
Long spreadSheet = new Long(541878439);
if (!dbData.equals(spreadSheet)) {
System.out.println("Please update your contact");
} else {
System.out.println("Not required");
}
}
output
Please update your contact
if(dbData.longValue() != spreadSheet.longValue())
Edit your condition as above. It will compare 2 long values. Your exiting condition compare 2 object references.
Thanks
You should compare two instances of Long-class only with it's equals method, because == operators check the addresses, not the values.
if(!dbData.equals(spreadSheet))
{
Log.i("","Please update your contact");
}
else
{
Log.i("","Not required");
}
Use Long.compare(long x, long y)
if (Long.compare(val1, val2) == 0) {
// values are equal
} else {
// values are not equal
}
Your dbData and spreadSheet are instances of the Long class, not long primitives. You have to compare theme either using the equals() method, or compare their longValue()s. I.e.
if (!dbData.equals(spreadSheet)) {
// ...
} else {
// ...
}
or
if (dbData.longValue() != spreadSheet.longValue()) {
// ...
} else {
// ...
}
You can use below code.
Long dbData = 54188439L;// Got the value at run time
Long spreadSheet = 54188439L;//Got the value at run time
if (!dbData.equals(spreadSheet)) {
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I got this. But my list is not empty and they have element with code "ADPL". Why this return me NoSuchElement ?
String retour = CodeExecutionChaine.A.getCode();
if (!lstChaines.isEmpty()) {
retour = lstChaines.stream()
.filter(t -> t.getNomChaine() == Chaines.ADPL.getCode())
.map(Chaine::getStatutChaine)
.findFirst()
.orElse(CodeExecutionChaine.A.getCode());
The enum Chaines
public enum Chaines {
ADPL("ADPL"),
ADIL("ADIL"),
ADSL("ADSL");
private String code = "";
Chaines(String code) {
this.code = code;
}
public String getCode() {
return this.code;
}
}
This is the same for CodeExecutionChaine
Change t -> t.getNomChaine() == Chaines.ADPL.getCode() to t -> t.equals(Chaines.ADPL.getCode()).
== checks for identity. Therefore, == will result into true only if two references point to the same object. On the other hand, equals checks for equality. Two references that don't point to the same object but have similar properties are still considered equal. You get a NoSuchElementException because you used == to filter your Stream which resulted in zero elements satisfying the condition.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Why is this coming out as false?
public class practice
{
public static void main(String [] args)
{
System.out.println(startHi("hi "));
}
public static boolean startHi(String str)
{
System.out.println(str.substring(0,2));
if(str.length() < 2)
{
return false;
}
else if(str.substring(0,2) ==("hi"))
{
return true;
}
else
{
return false;
}
}
}
You should use the .equals method to check for equality of strings, not ==. See here.
Using == is checking to see if the objects have the same address in memory. That's not usually what you're looking for when checking if the value of two strings are the same.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have this, in my onPostExecute :
String test = currentActivity.getClass().getSimpleName();
if(test == "SplashScreen"){
Log.e("OK",test);
} else {
Log.e("OK",test);
}
The problem is, the value of test is "SplashScreen" (that we can see with the log), but the code never goes in the if, only in the else.
Why does this happen?
As the others had already said you can not use the == operator on strings.
The reason for this is that strings are objects and not primitive datatypes.
With that said the == operator will check if the two strings has the same memory point in the memory if I'm not mistaken.
use equals to compare strings:
if(test.equals("SplashScreen"){
Log.e("OK",test);
} else {
Log.e("OK",test);
}
You can't use == on strings. You should use:
"SplashScreen".equals(test)
Test might be null, it is better to call equals() on "SplashScreen" since you know that it is no null.
Don't compare string with == use .equals()
String test = currentActivity.getClass().getSimpleName();
if(test.equals("SplashScreen")){
Log.e("OK",test);
} else {
Log.e("OK",test);
}
It's better to use equalsIgnoreCase(String string).
String test = currentActivity.getClass().getSimpleName();
if(test.equalsIgnoreCase("SplashScreen")){
Log.e("OK",test);
} else {
Log.e("OK",test);
}