This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm importing a string from a file and the string is "Computer_Made". If I execute this code, though, it does not print "The computer is already made!" Any ideas?
if (data=="Computer_Made")
{
computer=true;
System.out.println("The computer is already made!");
}
You should use .equals for string comparison!!
if (data.equals("Computer_Made"))
{
computer=true;
System.out.println("The computer is already made!");
}
Refer here for more info
In Java, String are compared using equals or equalsIgnoreCase method.
Using == is reference equality and will rarely be the same.
Try instead:
if (data.equals("Computer_Made"))
== will only work in example like this:
String a = "Ha";
String b = a;
System.out.println("a==b :" + a==b); //prints a==b : true
Related
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
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have previously asked a question about comparing 2 strings and was told that I should always use .equals.
However, I do not understand why this then works:
String y= "Mary";
String x= "Mary";
System.out.print(x==y);
This will print true, and I do not understand why.
Because those two String(s) have the same reference identity, and that is because they came from the String intern pool. If you were to add a new String() to one of them, like so -
String y= "Mary";
String x= new String("Mary");
System.out.print(x==y);
You would get false.
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");
}
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]))
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