CSE142 Program Example handout #18
Sample Log of Execution
-----------------------
This program will allow you to search the
imdb top 250 movies for a particular phrase.
search phrase? kill
Rank Votes Rating Title
40. 37815 8.5 To Kill a Mockingbird (1962)
88. 89063 8.3 Kill Bill: Vol. 1 (2003)
112. 64613 8.2 Kill Bill: Vol. 2 (2004)
128. 9149 8.2 Killing, The (1956)
Second Log of Execution
-----------------------
This program will allow you to search the
imdb top 250 movies for a particular phrase.
search phrase? 1940
Rank Votes Rating Title
74. 14607 8.4 Rebecca (1940)
104. 13824 8.3 Great Dictator, The (1940)
161. 13085 8.1 Philadelphia Story, The (1940)
171. 9862 8.1 Grapes of Wrath, The (1940)
222. 8664 8.0 His Girl Friday (1940)
First 25 Lines of Data File imdb.txt
------------------------------------
162918 9.1 Godfather, The (1972)
196376 9.1 Shawshank Redemption, The (1994)
93064 8.9 Godfather: Part II, The (1974)
147399 8.9 Lord of the Rings: The Return of the King, The (2003)
81507 8.8 Casablanca (1942)
121119 8.8 Schindler's List (1993)
43192 8.8 Shichinin no samurai (1954)
130687 8.8 Star Wars: Episode V - The Empire Strikes Back (1980)
170993 8.8 Pulp Fiction (1994)
166328 8.8 Star Wars (1977)
43489 8.8 Buono, il brutto, il cattivo, Il (1966)
87074 8.8 One Flew Over the Cuckoo's Nest (1975)
193332 8.8 Lord of the Rings: The Fellowship of the Ring, The (2001)
51262 8.7 Rear Window (1954)
127426 8.7 Usual Suspects, The (1995)
44283 8.8 Cidade de Deus (2002)
113309 8.7 Raiders of the Lost Ark (1981)
37945 8.7 12 Angry Men (1957)
78259 8.7 Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)
149581 8.7 Lord of the Rings: The Two Towers, The (2002)
73757 8.6 Citizen Kane (1941)
65817 8.6 Psycho (1960)
87395 8.6 Goodfellas (1990)
115455 8.6 Memento (2000)
43800 8.6 North by Northwest (1959)
Program File Movies.java
------------------------
// Stuart Reges
// 5/10/06
//
// This program prompts for a phrase and it prints all movies from the
// imdb top-250 database that contain the phrase.
import java.io.*;
import java.util.*;
public class Movies {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("imdb.txt"));
Scanner console = new Scanner(System.in);
System.out.println("This program will allow you to search the");
System.out.println("imdb top 250 movies for a particular phrase.");
System.out.println();
System.out.print("search phrase? ");
String phrase = console.nextLine().toLowerCase();
System.out.println("Rank\tVotes\tRating\tTitle");
int rank = 1;
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.toLowerCase().indexOf(phrase) != -1) {
print(rank, line);
}
rank++;
}
}
// Prints the rank and data for one line of the input file
public static void print(int rank, String line) {
Scanner data = new Scanner(line);
int votes = data.nextInt();
double rating = data.nextDouble();
System.out.print(rank + ".\t" + votes + "\t" + rating + "\t");
while (data.hasNext()) {
System.out.print(data.next() + " ");
}
System.out.println();
}
}