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.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I'm new to Java/programming and I'm trying to write a simple program that gets an element from a list IF that element is equal to some user input. I'm using a for-loop and if-statement to achieve this but even though the user input and element matches up the programming won't print the element to screen. If someone could explain why this is not working it would be very appreciated. Cheers
public static void main(String[] args){
ArrayList<String> names = new ArrayList<String>();
String tempObject;
String findName;
names.add("John");
names.add("Ronny");
names.add("Gona");
names.add("Gina");
Scanner Input = new Scanner(System.in);
System.out.print("Search list for: ");
findName = Input.nextLine();
for (int i = 0; i < names.size(); i++){
tempObject = names.get(i);
if (tempObject == findName){
System.out.print("\n" + tempObject);
}
}
}
Here you go:
if (tempObject.equals(findName)){
System.out.print("\n" + tempObject);
}
For objects, which String is, always use method equals(), since == will compare references, not values (or what is set in equals() method - in String, it will compare the size, and then compare each char on the same place if they are equal - also, if you need, you have a method called equalsIgnoreCase - sometimes, its better to use that for user inputs).
For primitives, you will have to use ==.
There is difference between equality and identity, in your code above you used identity instead of equality, if you change your code (to use equality) as the below you will get what you need
if (tempObject.equal(findName)){
System.out.print("\n" + tempObject);
}
You should use String equals to compare two Strings for equality, not operator == which just compares the references.
Try the if statement like this:
if (tempObject.equals(findName)){...}
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.
Well, I really tried to solve (and google) it, but I couldn't.
And sorry for my english and for that idiotic title (This one is the best that came to mind) :)
System.out.println("AM I A GENIUS?");
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
if (s == "yes"){
System.out.println("^_^");
}
else{
System.out.println("I am not a genius(");
}
console:
AM I A GENIUS?
yes
I am not a genius(
if (s == "yes") --->> NEVER
instead use
if (s.equals("yes"))
EDIT: just for explain: the first statement is a boolean operator that controls if the references of the object s and the object "yes" are the same.
the second one compares the real content of the String variable.
So, in general, you should never use boolean comparison when using not primitive types.
In fact, you are comparing references and not the 2 String objects.
What you should do is the following:
System.out.println("AM I A GENIUS?");
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
if (s.equals("yes")){
System.out.println("^_^");
}
else{
System.out.println("I am not a genius(");
}
The equals method of the class String now compares the Strings s and "yes" characterwise.
I think Roman was right, you should be using .equalsnot ==, they imply different things.
== checks the references to the objects, which can work depending on what what you're comparing, but it's best to use .equals() which compares the content of the strings.
Your code should look like this:
String s = scan.nextLine();
if (s.quals("yes")){
System.out.println("^_^");
}
else{
System.out.println("I am not a genius(");
}
I'd also recommend using a .toLowerCase() on the user input, because that also cause the conditional to return false when the user types "Yes" as supposed to "yes". That would look like: s=s.toLowerCase();
Hope this helps.
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))
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String is not equal to string?
I'm new to java and I can't figure out what's wrong with this code block.
I know the array isn't null I'm testing it elsewhere. Maybe there is a syntax problem I'm used to program in c#.
Scanner input = new Scanner(System.in);
System.out.println("Enter ID :");
String employeeId = input.nextLine();
int index = -1;
for(int i = 0 ; i < employeeCounter ; i++)
{
if(employeeId == employeeNumber[i])
{
index = i;
}
}
if(index == -1)
{
System.out.println("Invalid");
return;
}
I always get to the 'Invalid' part. Any idea why ?
Thanks in advance
employeeNumber[0] is "12345"
employeeId is "12345"
but I can,t get into the first if statement although employeeId IS equal to employeeNumber[0].
Don't compare strings with ==.
Use
if (string1.equals("other")) {
// they match
}
Compare strings like that
if(employeeId.equals(employeeNumber[i]) {
}
As others have pointed - full code will be helpful, but my guess would be this line of the code:
if(employeeId == employeeNumber[i])
You don't compare 2 strings by using ==. Use equals() or equalsIgnoreCase() instead. == only checks for object equality i.e. are employeeId and employeeNumber referencing to the same object in memory. So, for objects always use the equals() method..for Strings you can also use equalsIgnoreCase() for a case insensitive match. == should be used on primitive types like int, long etc.
When you use == with two string, it compares pointer addresses
You should use firststring.equals(secondstring) in order to compare two strings
Use equals() method to compare Strings
if(employeeId.equals(employeeNumber[i])){}
When you compare strings, use
String1.equals(String2);
This should give you the result
"==" checks whether the reference for two objects are same. But equals() method checks whether the content is same or different.