class TestStringCompares {
    static private String[] table = { "Alberta", 
                                      "British Columbia",
                                      "Inuvit",
                                      "Manitoba",
                                      "New Brunswick",
                                      "Newfoundland",
                                      "Northwest Territories",
                                      "Nova Scotia",
                                      "Ontario",
                                      "Prince Edward Island",
                                      "Quebec",
                                      "Saskatchewan",
                                      "Yukon" };
                               
     
    // Example pg 163-164 Java Programming Language
    //  Basic binary search algorithm to find a string within
    //  a table of strings                              
    static public int position(String key) {
        int lo = 0;
        int hi = table.length-1;
        while( lo <= hi ) {
            int mid = lo + (hi - lo)/2;
            int cmp = key.compareTo(table[mid]);
            if( cmp == 0 )          // found it!
                return mid;
            else if( cmp < 0 )      // search the lower part
                hi = mid - 1;
            else                    // search the higher part
                lo = mid + 1;            
        }                           
        return -1;                  // not found
    }
     
     
    // Modified Example pg 166 Java Programming Language
    //   Using intern() to build a table of unique keys
    
    static int TABLE_SIZE = 4;
    static String[] keys = new String[TABLE_SIZE];
     
    static void putIn(String key) {
        String unique = key.intern();
        boolean found = false;
        
        // see if it's already in the table
        for(int i=0; i< TABLE_SIZE; i++) {
            if( keys[i] == unique ) {
                found = true;
            } else if(keys[i] == null & !found) {
                keys[i] = unique;
                break;
            }    
        }
    }
      
    public static void main(String[] args) {
        System.out.println(position("Ontario"));        
        
        // Example pg 165 Java Programming Language
        String str = "Look, look";
        boolean b1, b2, b3;
        
        b1 = str.regionMatches(6,"Look",0,4);           // not uppercase
        b2 = str.regionMatches(true, 6, "Look", 0, 4);  // case ignored
        b3 = str.regionMatches(true, 6, "Look", 0, 5);  // not the same over 5 chars
        
        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
        System.out.println("b3 = " + b3);
        
        // Using intern() method to build a table of unqiue keys
        putIn("John");
        putIn("Paul");
        putIn("Ringo");
        putIn("John");
        putIn("Paul");
        putIn("George");
        
        
        for( int i=0; i<TABLE_SIZE; i++)
            System.out.println(keys[i]);
    }   
    
}