-1

So I have this block of code:

public Student(String name, String subject) {
        if(subject.substring(17,18) == "e"){
            subjectName = "English";
        }else if(subject.substring(17,18) == "m") {
            subjectName = "Math";
        }
        studentName = name;
    }

I am feeding the Student in a name as a string and then a subject which is in a form like this:

..\text\students\english\grades

Now I use substring on it and get only the first letter of the subject. If the letter is e then the lesson name is english and so on.

But for some reason my IF checks dont pass at all, if I throw a sysout in them, nothing happens, the check never passes while it should...

What could be the problem here?

Kaspar
  • 1,600
  • 4
  • 24
  • 46

1 Answers1

4

You are comparing String using ==, you need to use .equals() method for String

Try to change your code to something like this:

if(subject.substring(17,18).equals("e")){
        subjectName = "English";
    }else if(subject.substring(17,18).equals("m")) {
        subjectName = "Math";
    }
Salah
  • 8,567
  • 3
  • 26
  • 43