-1

When I take a String input using BufferedReader, for eg.:

String a=br.readLine();

and after that when I type:

 if(a=="hello")
    {//statements}
    else{....}

then even if the user enters 'hello' as string a, it never executes the statements in the if block and just jumps to else. I don't face this problem when I directly take a string input in the parameter list of a method without using buffered reader. What is wrong? and how else can I take string input using buffered reader without facing this problem?Please help. p.s. there is no syntax error in my program

1 Answers1

1

Use String#equals to check String content. The == operator is used to compare Object references.

if (a.equals("hello"))

or to avoid NPE, better to use

if ("hello".equals(a)))
Reimeus
  • 158,255
  • 15
  • 216
  • 276