Java Reference
In-Depth Information
This program completes three phases to obtain the information needed. First, the fa-
mous person is presented to Google to get a list of web sites that contain this person's name.
Secondly, each of the search results is scanned for a birth year. These birth years are accu-
mulated in a list. Finally, the program determines which birth year was the most prevalent.
This birth year is then assumed to be the birth year of the famous person.
This recipe begins by submitting the name of the famous person to Google. The results
are read by calling the getResults function.
search = new GoogleSearch();
search.setKey(key);
System.out.println("Getting search results form Google.");
Collection<GoogleSearchResultElement> c = getResults(name);
int i = 0;
Next, each of the URLs returned from Google are checked. Because some of the URLs
returned from Google may no longer be valid, a try/catch block is used inside of the
loop to catch errors. This is not really an issue to the program. Such URLs are simply skipped
and the loop continues to the next iteration.
Each URL that is found is passed to the checkURL method. This method will be cov-
ered in a later section.
System.out.println("Scanning URL's from Google.");
for (GoogleSearchResultElement element : c)
{
try
{
i++;
URL u = new URL(element.getURL());
System.out.println("Scanning URL: " + i + "/" + c.size()
+ ":" + u);
checkURL(u);
}
catch (IOException e)
{
}
}
Once all of the URLs have been processed, the getResult function is called to deter-
mine which birth year is the famous person's actual birth year.
int resultYear = getResult();
if (resultYear == -1)
{
Search WWH ::




Custom Search