0

I am going to check if a JLabel is the same as a name of a tv show.

I have the code

public int lastEp() {
    if(name.getText() == "Dexter") {
        switch (season) {
        case 1:
            return 12;
        case 2:
            return 12;
        // etc.
        }
    }
    return -1;
}

I have checked in console what i get from name.getText() and the console prints "Dexter". Still my if statement wont return true. System.out.println(name.getText() == "Dexter") gives false, while System.out.println(name.getText() + " " + "Dexter") gives "Dexter Dexter". What is happening?

Also bonus question, if anyone know what i should return instead of -1 if no other option fits, if there is a good standard to follow.

Danjoa
  • 143
  • 4
  • 14

1 Answers1

1

This is an extended comment and the question should be closed

Strings in Java are compared with String#equals not ==

For example,

"Dexter".equals(name.getText())

You are currently comparing the object (memory) reference, which will vary rarely be equal

Check out the Strings trail for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366