equal and it does not matter which goes first. Program Java1216.java in figure 12.21 uses the compareTo method in this fashion. The user will enter two String values. The method is used to them. We then check compareTo compare whether this int value returned is or 0, which lets us know positive, negative, which String alphabetically goes first. Figure 12.21 // Java1216.java // This program the <compareTo> method being used to alphabetize 2 strings. // Note: This will not work properly if the strings are not both in the same "case". public class Java1216 { public static void main (String args[]) { System.out.println("\nJava1216.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); second string ===>> "); String s2 = Expo.enterString(); System.out.println(); if (s1.compareTo(s2) < 0) System.out.println(s1 + " goes before " + s2); else if (s1.compareTo(s2) > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); System.out.println(); } } The last output of program Java1216.java may look a bit strange. It says goes before apple”. Since when does come before This “ZEBRA ‘Z’ ‘a’? problem was discussed back in Java1210.java. The solution to this problem is to use one of the very methods introduced in Java1210.java. Those methods are toUpperCase and toLowerCase. To preperly determine which string goes first, we need to convert all strings to all CAPITAL letters or all lowercase letters before you compare them. Program in figure 12.22 does this and Java1217.java now the output actually makes sense. Both String variables get converted to upper case. Notice that actually changes to in the output. “apple” “APPLE”
equal and it does not matter which goes first. Program Java1216.java in figure 12.21 uses the compareTo method in this fashion. The user will enter two String values. The compareTo method is used to compare them. We then check whether this int value returned is positive, negative, or 0, which lets us know which String alphabetically goes first. Figure 12.21 // Java1216.java // This program the <compareTo> method being used to alphabetize 2 strings. // Note: This will not work properly if the strings are not both in the same "case". public class Java1216 { public static void main (String args[]) { System.out.println("\nJava1216.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); second string ===>> "); String s2 = Expo.enterString(); System.out.println(); if (s1.compareTo(s2) < 0) System.out.println(s1 + " goes before " + s2); else if (s1.compareTo(s2) > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); System.out.println(); } } The last output of program Java1216.java may look a bit strange. It says “ZEBRA goes before apple”. Since when does ‘Z’ come before ‘a’? This problem was discussed back in Java1210.java. The solution to this problem is to use one of the very methods introduced in Java1210.java. Those methods are toUpperCase and toLowerCase. To preperly determine which string goes first, we need to convert all strings to all CAPITAL letters or all lowercase letters before you compare them. Program Java1217.java in figure 12.22 does this and now the output actually makes sense. Both String variables get converted to upper case. Notice that “apple” actually changes to “APPLE” in the output.
Text12-StringsMethods.docx