Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created August 7, 2019 09:55
Show Gist options
  • Save gauravkukade/bbb9755bd2c34d56521a43f107dd2d65 to your computer and use it in GitHub Desktop.
Save gauravkukade/bbb9755bd2c34d56521a43f107dd2d65 to your computer and use it in GitHub Desktop.
A Java program to compare two strings lexicographically using compareTo() library function. It is embedded at https://coderolls.com/compare-two-strings-lexicographically-in-java
/**
* A Java program to compare two strings lexicographically
* using compareTo() library function.
*
* @author Gaurav Kukade at coderolls.com
*/
public class CompareLexicographically {
public static void main(String[] args) {
String firstString = "Paneer";
String secondString = "Paneer";
String thirdString = "Butter";
String fourthString = "Cheese";
System.out.println("Comparing two strings lexicographically.");
System.out.print("\nCompairing character sequence of the firstString ("+firstString+") to the character sequence of the secondString ("+secondString+") returns: ");
System.out.println(firstString.compareTo(secondString));
System.out.print("\nCompairing character sequence of secondString ("+secondString+") to the character sequence of thirdString ("+thirdString+") returns: ");
System.out.println(secondString.compareTo(thirdString));
System.out.print("\nCompairing character sequence of thirdString ("+thirdString+") to the character sequence of fourthString ("+fourthString+") returns: ");
System.out.println(thirdString.compareTo(fourthString));
System.out.print("\nCompairing character sequence of fourthString ("+fourthString+") to the character sequence of firstString ("+firstString+") returns: ");
System.out.println(fourthString.compareTo(firstString));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment