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 52 8.4 77498 To Kill a Mockingbird (1962) 134 8.2 215209 Kill Bill: Vol. 1 (2003) 180 8.0 22821 The Killing (1956) 204 8.0 169157 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? 1939 # Rating Votes Title 110 8.2 31192 Mr. Smith Goes to Washington (1939) 120 8.2 96658 The Wizard of Oz (1939) 157 8.1 73537 Gone with the Wind (1939) 245 7.9 19391 His Girl Friday (1940) 4 matches First 25 Lines of Data File imdb.txt ------------------------------------ 1 475377 9.1 The Shawshank Redemption (1994) 2 383704 9.1 The Godfather (1972) 3 226364 9.0 The Godfather: Part II (1974) 4 144047 8.9 Il buono, il brutto, il cattivo. (1966) 5 386542 8.9 Pulp Fiction (1994) 6 255971 8.8 Schindler's List (1993) 7 105647 8.8 12 Angry Men (1957) 8 197249 8.8 One Flew Over the Cuckoo's Nest (1975) 9 260017 8.8 Star Wars: Episode V - The Empire Strikes Back (1980) 10 423683 8.8 The Dark Knight (2008) 11 338595 8.8 The Lord of the Rings: The Return of the King (2003) 12 303568 8.8 Star Wars (1977) 13 157822 8.7 Casablanca (1942) 14 211839 8.7 Goodfellas (1990) 15 90078 8.7 Shichinin no samurai (1954) 16 352047 8.7 Fight Club (1999) 17 152357 8.7 Cidade de Deus (2002) 18 229152 8.7 Raiders of the Lost Ark (1981) 19 363677 8.7 The Lord of the Rings: The Fellowship of the Ring (2001) 20 110538 8.7 Rear Window (1954) 21 250325 8.7 The Usual Suspects (1995) 22 133140 8.7 Psycho (1960) 23 67438 8.7 C'era una volta il West (1968) 24 227630 8.6 The Silence of the Lambs (1991) 25 355106 8.6 The Matrix (1999) 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(); } }
Stuart Reges
Last modified: Wed Feb 17 10:49:14 PST 2010