-1

What is the correct way to put string in a condition statement? I tried this, but it won't print x.

    String c = "c" ,d = "d";
    Scanner in = new Scanner(System.in);
    String selection = in.next();
    if ( c == selection){
        System.out.println("x");

Assume I input c.

1 Answers1

0

Strings in Java are compared using str.equals(<string to compare>);

So in your case it would be if(c.equals(selection)) not if(c==selection)

Chaos
  • 11,213
  • 14
  • 42
  • 69