if condition for String [duplicate] - java

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.

Related

How do you check if a string is not equal to an object? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I ave a code where I want to use != sign. But since I am using String, How do I not equals Sign. Hers is my code. So I want all this statement not to equal to each other so it can print Tie Game.
if (Array[0] == Array[currentPlayer] && Array [1] ==
Array[currentPlayer] && !Array [2] == Array[currentPlayer])
The above code is when everything equals to each other. But I want this statements not to equal each other.
Keep in mind that I have not used Int or Char, I am using String.
For string inequality, negate a call to the equals method using the !:
String x = "ABC";
String y = "XYZ";
if(!x.equals(y)) {
//do stuff
}
! can be used to negate ANY boolean expression, and String.equals returns a boolean.
You can do something like:
if (!Array[0].equals(Array[currentPlayer]) && !Array[1].equals(Array[currentPlayer])
&& Array[2].equals(Array[currentPlayer]))
Use equals() if you want case sensitive match meaning it will look at case of string as well when matching.
If you want case insensitive matching you can use equalsIgnoreCase() method in place of equals()

if statement issue with mysql getstring [duplicate]

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

Why does "T" not equal "T" in this example? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm trying to write a simple hangman game in java for my college class. For some reason the if statement never returns seems to think that the two substrings being compared are equal. The two print statements show that by all rights the two should equate.
public String guessLetter(String letter)
{
String blanks = "";
String theWord="FOO";
for(int i=0; i<=theWord.length()-1; i++)
{
System.out.print(letter.substring(0,1).toUpperCase());
System.out.print(theWord.substring(i,i+1)+ "\n");
if((letter.substring(0,1).toUpperCase())==(theWord.substring(i,i+1)))
{
blanks = blanks + theWord.substring(i,i+1);
}
else
{
blanks = blanks + "___ ";
}
}
return blanks;
}
EDIT - As a great many people have pointed out, when comparing Strings, one must use the equals method instead of ==. I was unaware.
You are comparing a String so use "String".equals() dont use ==
use like this:
if((letter.substring(0,1).toUpperCase()).equals(theWord.substring(i,i+1)))
Java dont have == for string
you must use string1.equals(string2) function
if((letter.substring(0,1).toUpperCase())==(theWord.substring(i,i+1))) \ this is wrong for strings
When you compare strings you should use .equals or .equalsIgnorecase
if((letter.substring(0,1).toUpperCase()).equals(theWord.substring(i,i+1)))
ans also checkout the difference between == and .equals in java good explanation is given there.

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