This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am going to check if a JLabel is the same as a name of a tv show.
I have the code
public int lastEp() {
if(name.getText() == "Dexter") {
switch (season) {
case 1:
return 12;
case 2:
return 12;
// etc.
}
}
return -1;
}
I have checked in console what i get from name.getText() and the console prints "Dexter".
Still my if statement wont return true.
System.out.println(name.getText() == "Dexter") gives false, while System.out.println(name.getText() + " " + "Dexter") gives "Dexter Dexter".
What is happening?
Also bonus question, if anyone know what i should return instead of -1 if no other option fits, if there is a good standard to follow.
This is an extended comment and the question should be closed
Strings in Java are compared with String#equals not ==
For example,
"Dexter".equals(name.getText())
You are currently comparing the object (memory) reference, which will vary rarely be equal
Check out the Strings trail for more details
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
Please check the following code snippet from button's onClick event handler.
The if statement does not execute even though its TRUE (because of relational operators == and && maybe)... but works fine when string method( .contains() ) used as shown at the end.
EditText uname = (EditText)findViewById(2);
EditText pwd = (EditText)findViewById(3);
TextView msg = (TextView)findViewById(4);
String a = uname.getText().toString();
String b = pwd.getText().toString();
if(a==("afnan")&& b==("secret")){
msg.setText("abc");
}else {
msg.setText("xyz " + a + b); // concatenated a and b to see their values and they are same as in IF statement so why does ELSE always get executed but not IF?
}
Replacing relational operators with the following works fine but if statement gets true for "afnanaaaaa" since it does contain "afnan" but not EXACLTY contain "afnan":
if(a.contains("afnan")&& b.contains("secret")){
msg.setText("Welcome !!!");
}else if (a!= "afnan" && b!= "secret"){
msg.setText("xyz ");
}
HELP PLEASE !!!!!!!!!!
You need to understand that Java compare by reference rather than by value in this case. The following code is displays the correct solution:
if(a.equals("afnan")&& b.equals("secret")){
msg.setText("abc");
}else {
msg.setText("xyz " + a + b); // concatenated a and b to see their values and they are same as in IF statement so why does ELSE always get executed but not IF?
}
Ordinary objects for comparison is to use the method equals(). (etc. a.equals(b)) For arrays and some class need static method. You may want to improve basics of Java with the help of books. About it (equals) writes in the early chapters.
This question already has answers here:
Integer wrapper class and == operator - where is behavior specified? [duplicate]
(2 answers)
Closed 7 years ago.
public void pop() {
int a = stack.peek();
int b = min.get(min.size()-1);
System.out.println("a:"+a+" "+"b:"+b);
if (a==b) {
System.out.println("111");
}
if (stack.peek()==min.get(min.size()-1)) {
System.out.println("222");
}
stack.pop();
}
I created a class called MinStack, here is the pop(), the variable stack is a Stack(Integer), and min is an ArrayList(Integer), but the second if stmt is not always working correctly.
I got console like this:
a:512 b:-1000
a:-1000 b:-1000
111
a:-1000 b:-1000
111
I think the "111" and "222" will always show together, but here is not.
If I change the second if stmt as stack.peek()-min.get(min.size()-1)==0, then it works correctly, why this happened?
Thanks in advance.
EDIT:
I know where is wrong, I have to use stack.peek().intValue()==min.get(min.size()-1).intValue(), cause they are Integer.
This is an issue of Integer comparison. stack.peek and min.get(min.size()-1) both return Integers. Even though they contain the same int value, they are not the same Integer instance, so the comparison returns false.
Change your code to :
if (stack.peek().equals(min.get(min.size()-1))) {
System.out.println("222");
}
The alternative comparison stack.peek()-min.get(min.size()-1)==0 returns true because here you are comparing two int primitives.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an issue with the if/else function in my Java code (Android / Eclipse).
When I'm in debug mode, checkGender is equal to "M", but goes directly to "else" instruction and considers that 'checkGender' is not equal to "M". I don't understand why? For information, searcher.getString("Gender") takes information from MySQL.
String checkGender = "";
checkGender = searcher.getString("Gender");
if(checkGender == "M")
{
bGenderM.setChecked(true);
}
else
{
bGenderF.setChecked(true);
}
Debug info :
checkGender = "M" (id=8300460...)
value[0] = M
If I replace "searcher.getString("Gender");" by a simple "M", it works.
Thanks for your help.
Tom
Actually best would be to do it "Yoda-style"
if("M".equals(checkGender)) {
//code for male
} else {
//code for female
}
That way you avoid potential NullPointerException for the case that checkGender == null
Use equals() method instead of == for String comparison.
if(checkGender.equals("M")) {
bGenderM.setChecked(true);
} else {
bGenderF.setChecked(true);
}
OR, use equalsIgnoreCase() method and it better...
if(checkGender.equalsIgnoreCase("M")) {
bGenderM.setChecked(true);
} else {
bGenderF.setChecked(true);
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
When I debug this code I noticed that the If statement does not ever switch the boolean type sales variable to true... This is bugging me because I know that answer = "y" when it gets to the If statement. Help please! and yes I did import java.util.Scanner
Scanner input = new Scanner(System.in);
boolean sales = false;
String answer;
System.out.print("Will you be calculating the Sales department pay aswell? (y or n):");
answer = input.nextLine().trim();
if (answer == "y")
{
sales = true;
}
i have
The correct way to compare strings is:
if (answer.equals("y"))
Notice that in Java equals() is used for testing equality between objects, whereas the == operator is used for testing identity. They're two different concepts, and most of the time you're interested in equality.
As the #MadProgrammer suggests, inverting the comparison order is a good idea - it'll be safer in case answer is null:
if ("y".equals(answer))
You should use equals() to compare strings:
if (answer.equals("y")) {
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))