How to Compare Alphabetically-Ordered Strings in Java
When you use text strings in your Java programs you may want to store or display them in alphabetical order. The string class in the Java language provides a number of methods for comparing strings alphabetically so this is generally a straightforward task. You do need to tailor the processing to the needs of your own application project but in general you can compare two Java strings in a few simple lines of code, determining whether one string is lower or higher alphabetically or whether two strings are equal.
Instructions
-
-
1
Create two strings in your Java program. You can use existing strings if you have any but to create two test string variables use the following syntax:
String firstWord = "bat";
String secondWord = "cat";You can change the content of these strings if you like. Each one contains a short series of characters for demonstration. You can call methods of the string class on either of these variables.
-
2
Carry out the comparison on your strings. When you compare two strings you call the "compareTo" method of one string, passing the other as a parameter. The method returns a primitive type integer value. Call the method, storing the result in a variable as follows:
int comparison = firstWord.compareTo(secondWord);
This variable will contain a negative value if the first string is lower alphabetically, a positive value if the second string is lower alphabetically or a value of zero if the strings are equal.
-
-
3
Implement a conditional test to tailor processing to the result of your comparison. Add the following statements:
if(comparison<0) System.out.println("First word is first alphabetically");
else if(comparison>0) System.out.println("Second word is first alphabetically");
else System.out.println("Words are equal alphabetically");This code outputs a simple message depending on the result of the comparison operation. If the first test returns a true value the content of the "if" section will execute and Java will ignore the other two lines. If the first test returns a false result Java will ignore the content of the "if" statement and carry out the "else if" test. If this test returns true its content will execute. If both tests return false the content of the "else" statement will execute with the only remaining possibility that the strings are equal.
-
4
Carry out your comparison without using case. If you want the string comparison to ignore the case of characters use the following alternative version of the method:
int comparison = firstWord.compareToIgnoreCase(secondWord);By default Java treats upper case values as being first alphabetically compared to lower case characters. For example, the original test on the following values would indicate that the second string is first alphabetically:
String firstWord = "bat";
String secondWord = "Hat";If you want Java to compare only on the basis of alphabetical ordering use the method that ignores case.
-
5
Save your Java code file. Compile and run your program to test it. You can add the following output statement to see the integer value resulting from the comparison operation at a glance:
System.out.println(comparison);Change the string values a few times, running your program repeatedly to give yourself a clear idea of how the methods work.
-
1
Tips & Warnings
Java stores text strings as arrays of characters with each character represented numerically.
If you are dealing with user input text make sure you carry out plenty of tests on your Java program.
References
Resources
- Photo Credit Jupiterimages/Comstock/Getty Images