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 71 8.4 144254 To Kill a Mockingbird (1962) 151 8.1 473881 Kill Bill: Vol. 1 (2003) 194 8.0 44845 The Killing (1956) 3 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 113 8.2 54823 Mr. Smith Goes to Washington (1939) 148 8.1 149240 Gone with the Wind (1939) 154 8.1 193823 The Wizard of Oz (1939) 3 matches First 25 Lines of Data File imdb.txt ------------------------------------ 1 1080451 9.2 The Shawshank Redemption (1994) 2 756937 9.2 The Godfather (1972) 3 493481 9.0 The Godfather: Part II (1974) 4 838201 8.9 Pulp Fiction (1994) 5 326117 8.9 The Good, the Bad, and the Ugly. (1966) 6 1054044 8.9 The Dark Knight (2008) 7 267114 8.9 12 Angry Men (1957) 8 550765 8.9 Schindler's List (1993) 9 765594 8.8 The Lord of the Rings: The Return of the King (2003) 10 822262 8.8 Fight Club (1999) 11 523913 8.8 Star Wars: Episode V - The Empire Strikes Back (1980) 12 792026 8.8 The Lord of the Rings: The Fellowship of the Ring (2001) 13 451007 8.7 One Flew Over the Cuckoo's Nest (1975) 14 469768 8.7 Goodfellas (1990) 15 162895 8.7 Sevensamurai (1954) 16 851240 8.7 Inception (2010) 17 589264 8.7 Star Wars (1977) 18 719729 8.7 Forrest Gump (1994) 19 777639 8.7 The Matrix (1999) 20 687312 8.7 The Lord of the Rings: The Two Towers (2002) 21 353136 8.6 Cidade de Deus (2002) 22 146978 8.6 C'era una volta il West (1968) 23 533370 8.6 The Silence of the Lambs (1991) 24 631704 8.6 Se7en (1995) 25 280307 8.6 Casablanca (1942) 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: Mon Nov 4 12:40:31 PST 2013