if statement issue with mysql getstring [duplicate] - java

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

Related

logic for the conditions in "if " [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
if(class1.getMethod1().subString(0,1) == "1") && !class1.getMethod1().subString(0,1).equalsIgnoreCase(str1.subString(0,1)(shld be 1 or 2 or 3 )
i.e., String value is 1 or 2 or 3, then I should skip the if condition
I am not able to give
!class1.getMethod1().subString(0,1).equalsIgnoreCase(str1.subString(0,1) == 1 or 2 or 3
Can someone help me with appropriate logic ?
You can use a regex to check if your string starts with 1,2 or 3 (or not)
if(!class1.getMethod1().matches("^(1|2|3).*$")){
//......
}
you don't need substring.
You could store the output of your string in a variable like so:
String myStr = class1.getMethod1().subString(0,1);
And then compare your string with your output by using the equals() method rather than ==
Then you can check multiple conditions by using ||
if(!myStr.equals("1") || !myStr.equals("2") || !myStr.equals("3")) {
// Run conditional code...
}
This will make it so the code in the if statement will only run if the output of myStr is not 1, 2 or 3, thus the if statement will be skipped if the string is 1, 2 or 3
Having several cases that should not be processed follows the pattern !A && !B && !C.
String name = class1.getMethod1();
if (!name.startsWith("1") && !name.startsWith("2") && !name.startsWith("3")) {
...
}
I would create util class with method like this:
boolean isAny(String parameter, String ... values) {
for(String value : values) {
if(parameter.equals(value) {
return true;
}
}
return false;
}
You could use it in if statement like this:
if(!YourUtil.isAny(class1.getMethod1().subString(0,1), "1", "2", "3")) {
//
}
It's nice because you don't have redundant || what could make Sonar happy.

JLabel not comparing to string [duplicate]

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

if condition for String [duplicate]

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.

If statement wont recognize string [duplicate]

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))

Very weird behaving in "IF STATEMENT" in Android [duplicate]

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.

Categories