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))
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm trying to check a string for a specific number but it doesn't seem to find it.
string = sc.next();
if(string != "-1")
return 0;
So when I enter -1 into the input, it just continues on without breaking the program. Am I doing something wrong here?
Instead of != you need to use the operator NOT ! and the method equals
So
string = sc.next();
if (!string.equals("-1"))
return 0;
Remember infact that the operators == (or !=) check for same (or not same) object. Instead equals method check that the internal value of the string is the same of the parameter.
If you are expecting a number I would read a number.
int num = sc.nextInt();
if (num != -1) return 0;
BTW: When you use == or != for String it compares only the references, not the contents of the Strings.
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:
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()
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.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String firstanswer = scan.nextLine();
if(firstanswer == answer2)
{
System.out.println("OK lets get started");
}
else
{
System.out.println("That is incorrect, of course you want to play");
}
//answer2 is set to "yes", i declared it above
make it firstanswer.equals(answer2) instead of firstanswer == answer2.
When you want to check for equality of String in java then use equals method instead of == operator.
equals method checks whether the contents of String objects are same
== operator checks whether both the reference variables refer to same String object
To understand about strings and equality, read String comparison with equals and assignment operator It will help you understand the concept well.
Use equals() instead of == to compare strings.
if(firstanswer.equals(answer2)) is what you're looking for.
firstanswer and answer2 are pointers to string objects. == checks to see whether the pointers are equal (whether they point to the same object), while equals() compares the contents of the two strings and returns a boolean representing whether or not the contents are equal.