This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
System.out.println(characters.get(selected).getName());
// The name printed is "Mario" But the if statement will not
// work as though the two strings are diffrent but they are the same.
if(characters.get(selected).getName() == "Mario"){
playSound("sounds/clickmario.wav");
}
Comparing two strings and when I debug the comparison is "Mario" to "Mario" so the if statement should be true but its false because nothing inside the if statement is being read. Why is this happening? I have tried assigning this .getname to a tempString and comparing it but still when they are the same string the statement results as false. Please help
You have to use .equals() for string comparison in java
if(characters.get(selected).getName().equals("Mario")){
playSound("sounds/clickmario.wav");
}
Refer this for String comparison.
and
for basics of String refer this.
use
if (stra === strb)
it should work in javascript, for java
if (stra.equals(strb))
Then it should work too
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I am encountering a weird problem. I was trying to compare two 128 bits strings and I believe every char of them match each other(and I tested comparing String.chartAt(#) several times) but when I do
if (String 1== String 2)
..
else
..
It went to else clause. Why is that?
When using == for comparison, you are checking if both variables refer to the same object (reference comparision). You should use strings equals() method in order to compare if they are equal in a sense they consist of same characters.
description of equals method in java documentation
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if
and only if the argument is not null and is a String object that
represents the same sequence of characters as this object.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String s="rahul".substring(0, 1);
s==Character.toString('r') //2nd
2nd evaluates to false but it should evaluate to true since i am converting 'r' to a String.
Why i am getting false as a result?
By doing this i am able to make the condition true
//char s=name.charAt(0);
// s=='r'
;
Try it as:
s.equals(Character.toString('r'). For more details What's the difference between ".equals" and "=="?
Instead of comparing Strings with == you should use equals().
String s="rahul".substring(0, 1);
System.out.println(s.equals(Character.toString('r')));
Use .equals instead, see code below:
//current code
System.out.println(s==Character.toString('r'));
//change to be as follows
s.equals( Character.toString('r'));
System.out.println(s.equals( Character.toString('r')));
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to do a comparison in Java with 2 strings containing a extended ASCII character.
boolean result = "éasdfasdf".substring(0,1).equals("é");
Can somebody explain why this results false? I think it has something to do with character encoding, but I can't figure out what exactly the problem is here...
Update: ideone.com does successfully run these 2 lines, so the problem is locally in my box. I think I found some more proof of that:
System.out.println("éb".charAt(1) == 'b');
Does also fails... Can it be the problem of 2 different character encodings?
Use
boolean result = "éasdfasdf".substring(0,1).equals("é")
And it will give expected result!The reason is simple - using '==' you compare objects by reference, not by value. So equals() solves this problem
This question already has answers here:
Comparing strings with == which are declared final in Java
(6 answers)
Closed 9 years ago.
while comparing to strings we can do using == or .equals()
In == we know that it checks for references but in .equals() it checks for contents.
So suppose if there are 2 strings say
String s="SO";
String s1="SO";
so in this case s1==s and s.equals(s1) both will give true.
But here it gives me false
So what I assume is + is high priority than ==
so in this case
System.out.println(""+s1==s);
it will be splitted like (""+s1)==s and now ""+s1 will be a new String and hence the new String will never be equal to s so its printing false
I am just interested to know whether I thought is right or not
""+s1 creates a new String Object on the heap (since it is not declared as final). So, the references will not be same.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i am trying to do a presence check in java on 10 JTextFields. I want it so that if all 10 of my textfield have something in them, it will do my code.
String input1 = tfQ1.getText();
String input2 = tfQ2.getText();
etc.
I have put
IF(input1==("")&&input2==("")&&input3==("")&&input4==("")&&input5==("")&&input6==("")&&input7==("")&&input8==("")&&input9==("")&&input10==(""))
{
//DO SCORES ETC
}
However, this doesnt do anything... (my button does not work weather there are things in the text fields or not)
Please and someone help with presence check validation? Thanks =)
instead of "==" operator you should use
input1.equals("")
if(input.equals("WhateverYouAreLookingFor")) {
//do this
}else {
//do this
}
== is a reference comparison, both objects point to the same location in memory. essentially it tests wether the two operands refer to the same object.
.equals() will only compare what is in the String. It can be overridden so two distinct objects can still be equal.