// Counts the number of words in Hamlet,
// reading the data from a URL instead of a local file.

import java.io.*;
import java.net.*;
import java.util.*;

public class CountWordsURL {
    public static void main(String[] args)
            throws IOException {
        URL url = new URL(
            "http://buildingjavaprograms.com/hamlet.txt");
        Scanner input = new Scanner(url.openStream());
        int count = 0;
        while (input.hasNext()) {
            String word = input.next();
            count++;
        }
        System.out.println("total words = " + count);
    }
}
