0
    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

3 Answers3

3

You have to use .equals() for string comparison in java

if(characters.get(selected).getName().equals("Mario")){
    playSound("sounds/clickmario.wav");
}
Vic
  • 8,261
  • 6
  • 42
  • 55
  • == only works on simple primatives. You need to use .equals to compare the String objects to one another. This is correct. – zgc7009 Oct 30 '13 at 03:44
1

Refer this for String comparison. and for basics of String refer this.

Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80
0

use

if (stra === strb)

it should work in javascript, for java

if (stra.equals(strb))

Then it should work too

lostincomputer2
  • 353
  • 4
  • 12