0

The answer should be good but it always gives output bad, I have tried everything Here is the code :

import java.util.*;
public class stringuses {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    String str=new String("i am ayush");
    System.out.println("Who am i ?");

    String ans=sc.nextLine();
    if(str==ans)
    {
        System.out.println("good");
    }
    else
    {
        System.out.println("bad");
    }



    }

}
Ayush Singh
  • 57
  • 1
  • 7

1 Answers1

0

You should not use == to compare Strings in Java because that compares the in-memory reference of the two String objects, not their values. Use the equals() method of the String object instead. So in your code, the usage of the equals() method to compare the str and ans Strings would be,

str.equals(ans)

Hope this helps you! If it did, please mark my answer as "Accepted". Good day!

Ihan Dilnath
  • 343
  • 2
  • 9