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

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");
}

Related

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 - Why a == "1" returns false [duplicate]

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

Comparing String with String Values [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java String.equals versus ==
I have a string called DomainType which usually holds values like "edu,com,org..etc" from a url. I used an if else statement to help check the data type and output a JOptionPane. For some reason whenever you type any domain type, It gives you the last option.
Here is a Snippet from the code I wrote:
DomainType = URL.substring(URLlength - 3, URLlength);
if (DomainType == "gov") {
JOptionPane.showMessageDialog(null, "This is a Government web address.");
}
else if (DomainType == "edu") {
JOptionPane.showMessageDialog(null, "This is a University web address.");
}
else if (DomainType == "com") {
JOptionPane.showMessageDialog(null, "This is a Business web address.");
}
else if (DomainType == "org") {
JOptionPane.showMessageDialog(null, "This is a Organization web address");
}
else {
JOptionPane.showMessageDialog(null, "This is an unknown web address type.");
}
so the DomainType gives me edu or com no problem but i think it's my if statement I'm not doing right.
When comparing strings, never use ==, use equals. So, instead of
DomainType == "org"
use
DomainType.equals("org")
Why? == will compare references. That means: memory values. And they may not be equal for your strings. equals will compare values, and that is what you want.
To compare content use equals, not == (which compares references):
if (DomainType.equals("gov")) {
On a side note, a mega-if is probably not the most elegant way to do that - http://www.antiifcampaign.com/ - sorry, just being pedantic.
The .equals() method is the right way to compare objects indeed.

Can't break the while loop [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
public void play () {
int anInteger;
//guess return code
int code;
while (true) {
String input=null;
input = JOptionPane.showInputDialog("Please enter an integer");
if (input == "-1") {
//JOptionPane.showMessageDialog(null, input);
System.exit(0);
break;
} else {
if (input==null) {
System.exit(0);
} else if (input.isEmpty()) {
continue;
} else {
anInteger = Integer.parseInt(input);
code = this.oneGuess (anInteger);
//JOptionPane.showMessageDialog(null, anInteger);
}
}
}
}
I want, if the user enter -1, show the program will not prompt the message box any more. Above is the code I have come up with, so far. Why it doesn't work?
String comparisons does NOT work with "==" operator, use "String.equals(Object)" function
input.equals("-1");
Better way would be
"-1".equals(input);
as it also takes care of null input
You are comparing strings, which are objects, with the == operator, which checks whether two object references refer to the same object instance. Instead you should use the equals method for comparing them.
There is a difference between comparing with == and equals. The first compares pointers, the latter contents. That is probably your issue.
You compare Strings with ==, which creates a problem. You can have many different String-Objects which all show "-1". The == tests, if you have exactly the same object on the left and right side. You want to know, if the objects on the left and right sie have an equal content.
Better try
input.equalsIgnoreCase("-1");
EDIT: To answer the comment: input.equalsIgnoreCase("-1") is the same as input.equals("-1") in the case of "-1" as there are no uppercase/lowercase letters in "-1". However, I prefer equalsIgnoreCase in the case of Strings, because it is defined on String, rather than on Object. Still, as the equals-definition is overridden for the String class, it works too in this example and "ignoreCase" is not needed.

Java Scanner Class [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am writing a program that should close the console if the user input the String "end'.
The program always performs the else loop even if the user inputs "end". I'm wondering why the program is not getting into the if part of the loop and shutting down.
Scanner scan = new Scanner(System.in);
while(true)
{
String num = scan.nextLine();
if(num == "end")
{
System.exit(0);
}
else
{
System.out.println("hi");
}
}
You're using == instead of "end".equals(num)
Don't use == for equality of string as it compares the objects not the string itself.
Use num.equals("end") or num.equalsIgnoreCase("end") if you want to be able to type end or END
I would not use "end".equals(num), although considered better from a performance perspective in most cases, it does not clearly state the business requirement and it is more important to make it more readable.
But be aware of num being null, if that is possible, num.quals("end") could throw an exception and you should write if (num!=null && num.equals("end")) { ... }
Note that "end".equals(num) does not need the null check, but I still believe this is not very readable, so I would go with if (num!=null && num.equals("end")) { ... }
For testing equality between strings, you should use equals() instead.
if(a.equals(b)) and so on.
This should help you out: http://leepoint.net/notes-java/data/expressions/22compareobjects.html
In Java, you test equality of strings with:
string1.equals(string2);
So in this case, it would be:
num.equals("end");
Or to avoid an exception of type NullPointerException:
"end".equals(num);
num refers to the object, so num == "end" should never be. You want num.equals("end")
Please do not use comparison (==) operator when comparing objects in Java. Use equals(Object) instead.

Categories

Resources