← All resources

Write a program in Java to input two strings and print whether they are exactly equal or not.

import java.util.*;
public class String_Compare
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        String s1, s2;
        int l1, l2, i, flag=1;
        System.out.println("Enter 2 strings:");
        s1 = sc.nextLine();
        s2 = sc.nextLine();
        l1 = s1.length();
        l2 = s2.length();
        if(l1==l2)
        {
            for(i=0;i<l1;i++)
            {
                if(s1.charAt(i)!=s2.charAt(i))
                {
                    flag = 0;
                    break;
                }
            }
            if(flag==1)
            System.out.println("The strings are equal.");
            else
            System.out.println("The strings are not equal.");
        }//end of if
        else
        System.out.println("The strings are not equal.");
    }
}