MovieStats.java


Below is the syntax highlighted version of MovieStats.java from §4.5 Case Study: Small World.



/*************************************************************************
 *  Compilation:  javac MovieStats.java
 *  Execution:    java MovieStats data.txt 
 *  Dependencies: Graph.java In.java
 *
 *  Computes number of movies, actors, and edges in a given data file.
 *  
 *************************************************************************/

public class MovieStats {

    public static void main(String[] args) {

       // set of actors
       SET<String> actors = new SET<String>();
       int movies = 0;
       int edges = 0;

       // read in data and initialize graph
        In data = new In(args[0]);
        while (data.hasNextLine()) {
            String line = data.readLine();
            String[] names = line.split("/");
            String movie = names[0];
            movies++;
            edges += names.length - 1;
            for (int i = 1; i < names.length; i++)
                actors.add(names[i]);
        }
        StdOut.println(movies + " movies");
        StdOut.println(actors.size() + " actors");
        StdOut.println(edges  + " edges");
    }
}


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:17:30 EST 2011.