0

I was trying to compare a user input with a predefined string. But it did not show my expected result. Here is the code

import java.util.Scanner;
public class StringMatching {

    public static void main(String[] args) {
        Scanner scannerObj=new Scanner(System.in);
        String userinput=scannerObj.nextLine();
            System.out.println(userinput);

        System.out.println(userinput);
        if(userinput=="yes")
        {
            System.out.println("Yes! Working..");
        }
        else
        {
            System.out.println("Not Working..");
        }


    }
}
The output of this program shows Not Working. I don't know what I missed here. Looking for your kind support. Regards
ucMedia
  • 4,105
  • 4
  • 38
  • 46
Md Rashedul Hoque Bhuiyan
  • 10,151
  • 5
  • 30
  • 42

1 Answers1

1

In Java == compares whether the two references refers to the same object. It does not check whether the content of the string is equal.

As already stated, use String.equals(otherString) instead

Scherling
  • 1,350
  • 2
  • 13
  • 23