// (from the Carter book)
// using the "compareTo" String method in decision-making.
// The method "compareTo" returns an integer that indicates
// the lexicographic ordering of 2 strings. If "s1" and "s2"
// are both references to 2 strings, then the expression:
// s1.compareTo (s2);
// has the value 0 if "s1" is identical to "s2", a negative
// value if "s1" precedes "s2", and a positive value if "s1"
// follows "s2" alphabetically.
import java.awt.*;
import hsa.Console;
public class DecisionsWithStringsEx2
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
//1(a):
"cab".compareTo ("car");
// yields < 0 because the 'b' in "cab" < the 'r' in "car"
//1(b):
"Car".compareTo ("car");
// yields < 0 because the 'C' in "Car" < the 'c' in "car"
//1(c):
"27".compareTo ("186");
// yields > 0 because the '2' in "27" > the '1' in "186"
//1(d):
"car".compareTo ("cart");
// yields < 0 because "car" is only part of the string\
// "cart" ("car" and "cart" are different lengths)
} // main method
} // DecisionsWithStringsEx2 class
No comments:
Post a Comment