CSE142 Sample File-Processing Program handout #13
Sample Log of Execution
-----------------------
This program will allow you to search the
imdb top 250 movies for a particular phrase.
search phrase? kill
# Rating Votes Title
48 8.5 68297 To Kill a Mockingbird (1962)
138 8.2 186221 Kill Bill: Vol. 1 (2003)
156 8.1 19396 Killing, The (1956)
214 8.0 144949 Kill Bill: Vol. 2 (2004)
4 matches
Second Log of Execution
-----------------------
This program will allow you to search the
imdb top 250 movies for a particular phrase.
search phrase? 1940
# Rating Votes Title
80 8.3 28231 Rebecca (1940)
94 8.3 28587 Great Dictator, The (1940)
154 8.1 18528 Grapes of Wrath, The (1940)
220 8.0 16525 His Girl Friday (1940)
230 8.0 22823 Philadelphia Story, The (1940)
5 matches
First 25 Lines of Data File imdb.txt
------------------------------------
1 406283 9.1 Shawshank Redemption, The (1994)
2 340193 9.1 Godfather, The (1972)
3 196418 9.0 Godfather: Part II, The (1974)
4 118517 8.9 Buono, il brutto, il cattivo., Il (1966)
5 344328 8.9 Dark Knight, The (2008)
6 334886 8.9 Pulp Fiction (1994)
7 221858 8.8 Schindler's List (1993)
8 169931 8.8 One Flew Over the Cuckoo's Nest (1975)
9 85454 8.8 12 Angry Men (1957)
10 230925 8.8 Star Wars: Episode V - The Empire Strikes Back (1980)
11 140486 8.8 Casablanca (1942)
12 272663 8.8 Star Wars (1977)
13 79772 8.8 Shichinin no samurai (1954)
14 295789 8.8 Lord of the Rings: The Return of the King, The (2003)
15 183897 8.7 Goodfellas (1990)
16 96109 8.7 Rear Window (1954)
17 125645 8.7 Cidade de Deus (2002)
18 206185 8.7 Raiders of the Lost Ark (1981)
19 56576 8.7 C'era una volta il West (1968)
20 324142 8.7 Lord of the Rings: The Fellowship of the Ring, The (2001)
21 303065 8.7 Fight Club (1999)
22 222567 8.7 Usual Suspects, The (1995)
23 116574 8.7 Psycho (1960)
24 199751 8.6 Silence of the Lambs, The (1991)
25 42572 8.6 Sunset Blvd. (1950)
Program File Movies.java
------------------------
// 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);
giveIntro();
System.out.print("search phrase? ");
String phrase = console.nextLine().toLowerCase();
String line = find(input, phrase);
int count = 0;
if (line.length() > 0) {
System.out.println("#\tRating\tVotes\tTitle");
while (line.length() > 0) {
print(line);
line = find(input, phrase);
count++;
}
}
System.out.println(count + " matches");
}
// introduces the program to the user
public static void giveIntro() {
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();
}
// searches for and returns the next line of the given input that contains
// the given phrase; returns an empty string if not found
public static String find(Scanner input, String phrase) {
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.toLowerCase().contains(phrase)) {
return line;
}
}
return "";
}
// Prints the rank, rating, votes, and title separated by tabs for the
// given line of the input file
public static void print(String line) {
Scanner data = new Scanner(line);
int rank = data.nextInt();
int votes = data.nextInt();
double rating = data.nextDouble();
System.out.print(rank + "\t" + rating + "\t" + votes + "\t");
while (data.hasNext()) {
System.out.print(data.next() + " ");
}
System.out.println();
}
}