This question already has answers here:
How do I compare strings in Java?
(23 answers)
String.equals versus == [duplicate]
(20 answers)
Closed 9 years ago.
Anybody ever seen the following statement running false ?
String BloodyHell = "Unbelievable";
if (BloodyHell == BloodyHell) >> false
Although instead of bloody hell, I've got: |-,-| or |o,-| or |-,o| or |o,o| or XXXX.
None of these comes TRUE though. While debugging the exact thing is still false.
Somebody please help me out here. Here's the snippet:
public String doStats()
{
String[] pattern = splitPattern();
for (int i = 0; i < pattern.length; i++)
{
if (pattern[i] == "|-,-|")
frontClosed++;
if (pattern[i] == "|o,-|")
left++;
if (pattern[i] == "|-,o|")
right++;
if (pattern[i] == "|o,o|")
frontOpened++;
if (pattern[i] == "XXXX")
noFace++;
}
}
In Java you should use the String#equals method to compare string values instead of ==. The == operator compares object references to see if they are the same object, which is why you keep getting false. The object references are different, even if the contents of the strings are equivalent.
if (pattern[i].equals("|-,-|"))
There is a difference between == and equals().
When comparing strings, you want to use equals(); == is used to compare if they are the same thing in memory.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String s1 = "abcdef";
String s3 = new String(s1);
if(s3 == s1){
System.out.printf("yes");
}else{
System.out.printf("no");
}
Why is not print yes? Who can explain to me?
Change s3 == s1 to s3.equals(s1).
The == operator or will check if they're both the same object, rather than what their string value is.
When you are dealing with Objects, you should use their equals method, not the ==
Check this for further explanation:
Java String.equals versus ==
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
How can it be possible, that If statement doesn't understand two equal things that they are the same?
Situation:
I write into txfName field only letter b and then push button "Ok".
Code:
String letter = "b";
boolean same = false;
if (letter == txfName.getText()) {
same == true;
}
After if statement program shows me that variable same is false. Why? How can it be possible?
If I write code like this:
String letter = "b";
boolean same = false;
if (letter == "b") {
same == true;
}
Then after if statement program shows me that variable same is true. I don't understand, how it can be possible.
== compares to see if two objects are the same. When you are dealing with strings they are objects, so they may not have the same reference event though they can have the same value. You want to use .equals() instead.
For more details, Strings are special in java, as there are some internal workings that have a String pool. So in some cases the == may actually seem to be working, but in other cases it may not be. The reason is the String pool tries to cache recently used Strings to reduce the memory overhead. Anyway .equals() is what you are looking for.
for your first question
String letter = "b";
boolean same = false;
if (letter.equals( txfName.getText())) {
same = true;
}
return same;
will return true if txfName.getText() returns "b"
To compare objects in java use .equals() method instead of "==" operator
Replace the following code
if (letter == txfName.getText())
to
if (letter.equals(txfName.getText()))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I wanna ask you ,
do you have a solution for this question (if statement) or where is the error ?
String z = input.nextLine() ; //i want from the user value of z .
if (z ==( "Y" || "y" ))
{
statement ...
}
else if (z==("N" || "n" ))
{
statement ...
}
How to write a condition for if when the condition of String in Java ?
you can say
if(z.equalsIgnoreCase("y")){
//code...
}
You are probably looking for equalsIgnoreCase. You can use it like
if (z.equalsIgnoreCase("Y")){//do your job
use equals().
== does reference equality which you don't want.
So use equals() as you want to check the content equality.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String firstanswer = scan.nextLine();
if(firstanswer == answer2)
{
System.out.println("OK lets get started");
}
else
{
System.out.println("That is incorrect, of course you want to play");
}
//answer2 is set to "yes", i declared it above
make it firstanswer.equals(answer2) instead of firstanswer == answer2.
When you want to check for equality of String in java then use equals method instead of == operator.
equals method checks whether the contents of String objects are same
== operator checks whether both the reference variables refer to same String object
To understand about strings and equality, read String comparison with equals and assignment operator It will help you understand the concept well.
Use equals() instead of == to compare strings.
if(firstanswer.equals(answer2)) is what you're looking for.
firstanswer and answer2 are pointers to string objects. == checks to see whether the pointers are equal (whether they point to the same object), while equals() compares the contents of the two strings and returns a boolean representing whether or not the contents are equal.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an if statement that takes a string, and if another string has the same value as that string do 1 thing, and if the variable doesnt equal that string do another thring
here is my code
if(Pos != "D"){
System.out.println("doesnt = D");
}
if (Pos == "D" ){//WHY ISNT THIS WORKING
System.out.println("it does = D");
}
It recognizes when the variable doesnt = D and prints "doesnt = d" but when the variable = D it does nothing. I dont know why.
thanks
Never compare Strings with == or != since these check to see if two String variables refer to the same object reference, and this is not what you're interested in. Instead use the equals(...) or equalsIgnoreCase(...) method to see if the two Strings have the same chars in the same order as that's what really matters here. i.e.,
Use equals to compare strings :
if ("D".equals(Pos))