JAVA - Why a == "1" returns false [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String is not equal to string?
What makes reference comparison (==) work for some strings in Java?
can some one explain me following java code
String a = "1";
if(a == "1") {
//print compare 1 is true;
} else {
//print compare 1 is false;
}
if(a.equals("1")) {
//print compare 2 is true;
} else {
//print compare 2 is false;
}
it results like
compare 1 is false
compare 2 is true
Only explanation i have is that its comparing the memory address not the values itself. But i am not sure. can some please put a light on it. in .Net == operator is overloaded to compare contents of string.

use "1".equals(a) , String is an object so use equals() to compare

I understood that == operator is compare "Is it same object?"
object a is not same object with constant string "1".
so returns false

Related

Exit array for loop Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Consider my code below:
System.out.println("Insert your inventory");
for (int i = 0; i<20;i++) {
System.out.print(i+1+".");
if (inventory[i] == "N" || inventory[i]=="n") {
break;
}
inventory[i] = s.nextLine();
}
How can I exit from this loop if the user enters 'N' or 'n'?
You're comparing string with == operator. It does not yield correct result because your constant string "N" and your input "N" do not have same reference/pointer.
You have to use equals() to guarantee the correct compare result between strings.
Replace
if (inventory[i] == "N" || inventory[i]=="n")
With
if (inventory[i].equals("N") || inventory[i].equals("n"))
You should compare your String variables with the .equals() method instead of the == operator.
An explanation about why this is important can be found here on StackOverflow.

If Else Statement Not Returning Right Value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am trying to check if a playlist is simple or master by an index value. My problem is when I put the (True) URL it still returns a false statement "This is a simple Playlist".
Any tips on how I can fix this ?
String output = getPlaylistUrl(input);
String mediaRecord = output.substring(399);
String lastRecord = "gear4/prog_index.m3u8";
if (mediaRecord == lastRecord) {
System.out.println("This is a master playlist");
} else {
System.out.println("This is a simple playlist");
}
In Java, strings can not be compared for equality using ==, because == compares two instances, not the content. So unless s1 and s2 are actually the same instance, s1 == s2 will never return true.
You need to use equals(...) to compare two strings for equality.
if (mediaRecord.equals(lastRecord) { ... }
In order to compare Strings you need to use .equals and not ==. Using == compares references and not values

Java Check If Two JTextField Has The Same Content [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have two JTextFields txf1 and txf2.
In both of them I input the same content (for example: "test").
I made and If statement:
if (txf1.getText() == txf2.getText()) {
System.out.println("Equal");
} else {
System.out.println("Error");
}
Why it prints out the error message? I even made a System.out.println(txf1.getText()) and System.out.println(txf2.getText()) and the same looks equal, but prints out the error message?
String comparison in Java is done using String#equals, using == means you are comparing the memory reference of the objects, which won't always return true when you think it should.
Try something more like....
if (txf1.getText().equals(txf2.getText())) {
...instead
Also you can use this good practice which makes your text box entries efficient.
if (txf1.getText().trim().equals(txf2.getText().trim())) {
Use the equals method to compare Strings. == only compares the object reference. equals compares the actual content of the Strings.
Your code should be something like this:
if (txf1.getText().equals(txf2.getText())) {
System.out.println("Equal");
} else {
System.out.println("Error");
}

I entered one letter in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
How do I know if String letter is equal to char array
String[] choose = {"a","d","t","b","s","f","x"};
String check;
boolean error = false;
System.out.print("Enter");
Scanner sn = new Scanner(System.in);
check = sn.nextLine();
for (int i = 0 ; i < choose.length;i++){
if(choose[i] == check){
System.out.print("you entered" + choose[i]);
break;
}
}
What I did is this it didnt confirm I input letter a but "you entered" didnt show up.
You cannot test strings for equality using ==. That only compares references (memory addresses). You need to use String#equals(Object). In general == is most certainly what you don't want if you are testing for equality, unless you are checking to see if two variables are pointing to the same instance. This is rarely the case, since you are usually interested in testing values for equality.
So what you need to do is:
if(choose[i].equals(check)) {
...
}
You are trying to compare strings with ==, which only compares the references, but not the values. What you want to use is
if(check.equals(choose[i]))

Identical strings comparison gives me false [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I have two identical strings, one in an array and one in a String variable. When I compare these IDENTICAL strings I get false every time. I have debugged and debugged, but I get the same result every time. Here is the code in question
String temp = ""+(num1*num2);
Boolean equal = temp == answers[i];
if(equal) {
correct[i] = true;
num_correct ++;
}else{
correct[i] = false;
}
Again, I have debugged every minor detail of this program and I am 101% sure that the strings are IDENTICAL. Why is Java returning false on comparison?
When you use the == operator in Java with objects, you are attempting to compare object references. That is, is this object handle pointing to the EXACT same object as this other object handle. Unless the strings are interned, this will not work.
Use String.equals(Object) instead:
Boolean equal = temp.equals(answers[i]);
You are doing reference comparison, not value comparison. When you use the == operator its checking to see if the references are equal, and they aren't. If you want to check whether the values are equal use the equals method.
boolean equal = temp.equals(answers[i]);
== in java for strings is comparing to see if they are the same object, not the same string value. You should use .equals instead which will compare the value. == works sometimes because the strings can be interned and refer to the same object via reference even if created seperately through the same literal (so string b = "Hey" and string c = "Hey" end up being the same object in the background because "Hey" got interned to a hidden string object).
As others have shown you should use equals.
But I would also use the booleanValue of the Boolean object.
Here is your code correctly done
String temp = ""+(num1*num2);
Boolean equal = temp.equals(answers[i]);
if(equal.booleanValue()) {
correct[i] = true;
num_correct ++;
}else{
correct[i] = false;
}
Does this help?
Boolean equal = (temp == answers[i]);
I'm not sure that would be an issue, but I always enclose my conditions in parenthesis.

Categories

Resources