Java Program

Object:-Write a Program in java to compare two string

import java.util.Scanner;

public class JavaProgram

{
   public static void main(String args[])
   {
      String str1, str2;
      Scanner in = new Scanner(System.in); 
      System.out.println("Enter First String");
      str1 = in.nextLine(); 
      System.out.println("Enter Second String");
      str2 = in.nextLine(); 
      if(str1.compareTo(str2) > 0)
      {
         System.out.println("First String is Greater than Second String.");
      }
      else if(str1.compareTo(str2) < 0)
      {
         System.out.println("First String is Smaller than Second String.");
      }
      else
      {
         System.out.println("Both Strings are Equal (i.e. String1 is Equal to String2)");
      }
   }
}

Output

Enter First String
Rohit Sharma

Enter Second String
Amit

First String is Greater than Second String.
 

Explanation

To compare two string in Java Programming, you have to ask to the user to enter the two string and start comparing using the compareTo() method.








You may also like: