142. JAVA Program to compare two strings are same or not.
How does this program work?
- This program is used to compare two strings are same or not using java.
- By using if condition we can compare the given two strings.
Here is the code
//To compare two strings are same or not
import java.util.Scanner;public class compare
{
public static void main(String args[] )
{
String s1,s2;
System.out.println("Enter a string:" );
Scanner obj = new Scanner(System.in );
s1=obj.nextLine();
s2=obj.nextLine();
if(s1.equals(s2))
{
System.out.println("Both strings are equal." );
}
else
{
System.out.println("Both strings are not equal." );
}
}
}