/** An instance houses methods for searching a list
  * of top 101 cities with highest percentage of masters/phds */
// Uses class In; save In.java to this directory
public class AdvancedDegreesData {
    
    private static final String cityDataURL= "http://www.city-data.com/top2/h180.html";
    
    /** given an (html-formatted) entry in the table at cityDataURL, 
      * = <city name> (pop. <population>): <percent higher degrees>% */
    private static String extractCityInfo(String entry) {
        String cityStarter="html\">";
        String cityEnder="</A>";
        
        return entry.substring(entry.indexOf(cityStarter) + cityStarter.length(),
                               entry.indexOf(cityEnder)) 
            + " (" 
            + entry.substring(entry.indexOf("pop"), entry.lastIndexOf("</td>"));       
    }
    
    
    
    /** = info of first match of city name in top 101 cities in the US
      * with respect to percentage of population with 
      * advanced degrees ("not in list" if not in list)
      * Precondition: city is a valid city name 
      * (can be "East Ithaca, NY" or "East Ithaca") */
    public static String cityLookup(String city) {
        
        // extract webpage data into ranking
        String temp= (new In(cityDataURL)).readAll();
        String tableStarter="<div>";
        String tableEnder="<td width=\"350\"";
        temp= temp.substring(temp.indexOf(tableStarter), temp.indexOf(tableEnder));
        String[] ranking= temp.split("<div>"); // a VERY useful method!
        
        /* assert: ranking[0] has "<div>", 
           ranking[1] has data on Cayuga Heights, ...
           ranking[101] has data on North Potomac.
           This means ranking index indicates position in the ranking.*/
        
        
        /* store in i index of first city match in ranking, or
         ranking.length if city not there */
        
        
        // inv: city doesn't match ranking[0..i-1]
        int i;
        for (i=1; i!= ranking.length && ranking[i].indexOf(city)==-1; i= i+1) {
            ;
        }
             
        /* Post: city doesn't match ranking[0..i-1]
           and either city matches ranking[i] or 
           i is ranking.length */
               
        
        return (i < ranking.length?
                    i + ": " + extractCityInfo(ranking[i])
                    : "not in list"); 
        
    }
    

}