This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am working on a multiplayer game.. and i came up with a really weird situation.
It's even possible? -
System.out.println(foo); // left
System.out.println(foo instanceof String) // true
System.out.println(foo == "left") // false
I don't understand how it's even possible..
I really don't think it will be a good idea to post all of my code.
Any ideas why its like that? and how can i fix it?
Thanks in advance
You shouldn't compare Strings by == operator, use equals method instead.
Related
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
I am confused by this statement. Please help me explain this statement:
return third == Long.MIN_VALUE ? (int)first : (int)third;
Considering your top tags in SO is Python, let's explain this statement with Python:
return first if third == sys.maxint else third
Of course the Long.MIN_VALUE is not necessarily equal to sys.maxint in Python.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
I'm trying to make a user validation for multiple choices using a while loop and If the user doesn't match the choices then the program has to terminate. This is what I got so far
while(input!="air" || input!="water" || input!="steel"){
System.exit(0);
}
Unfortunately, it doesn't work.
I would prefer to do exceptions, but my professor is strictly against using it for this assignment. I really want to grow as a programmer so any advice, tips and tricks are welcomed, thank you all for trying to help me out.
Change it as
while(!input.equals("air") && !input.equals("water") && !input.equals("steel")){
System.exit(0);
}
Or
while(!(input.equals("air") || input.equals("water") || input.equals("steel"))){
System.exit(0);
}
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:
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
This question already has answers here:
Best way to "negate" an instanceof
(9 answers)
Closed 2 years ago.
Is it possible to get the opposite of instanceof in java? I have tried code like this:
if( example !instanceof blarg)....
but it won't let me put the ! anywhere without an error, please help.
You have to negate the entire thing:
if(!(example instanceof blarg))
You could also write it like so:
if(example instanceof blarg == false)
As an alternative make use of isInstance method:
if (!example.class.isInstance(blarg))
{
// your code here
}