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 or year. search phrase? 1939 # Rating Title (Year) 165 8.1 Gone with the Wind (1939) 199 8.1 Mr. Smith Goes to Washington (1939) 2 matches Second Log of Execution ----------------------- This program will allow you to search the imdb top 250 movies for a particular phrase or year. search phrase? kill # Rating Title (Year) 119 8.2 To Kill a Mockingbird (1962) 176 8.1 Kill Bill: Vol. 1 (2003) 2 matches First 25 Lines of Data File imdb.txt ------------------------------------ 9.2 1994 1 The Shawshank Redemption 9.1 1972 2 The Godfather 9.0 1974 3 The Godfather: Part II 9.0 2008 4 The Dark Knight 8.9 1957 5 12 Angry Men 8.9 1993 6 Schindler's List 8.9 2003 7 The Lord of the Rings: The Return of the King 8.8 1994 8 Pulp Fiction 8.8 1966 9 The Good, the Bad and the Ugly 8.8 2001 10 The Lord of the Rings: The Fellowship of the Ring 8.8 1999 11 Fight Club 8.8 1994 12 Forrest Gump 8.7 2010 13 Inception 8.7 1980 14 Star Wars: Episode V - The Empire Strikes Back 8.7 2002 15 The Lord of the Rings: The Two Towers 8.6 1999 16 The Matrix 8.6 1990 17 Goodfellas 8.6 1975 18 One Flew Over the Cuckoo's Nest 8.6 1954 19 Seven Samurai 8.6 1995 20 Se7en 8.6 1997 21 Life Is Beautiful 8.6 2002 22 City of God 8.6 1991 23 The Silence of the Lambs 8.6 1946 24 It's a Wonderful Life 8.6 1977 25 Star Wars: Episode IV - A New Hope Program File Movies.java ------------------------ // This program prompts for a phrase or year and it prints all movies from the // imdb top-250 database that contain the phrase or were made that year. 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\tTitle (Year)"); 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("or year."); 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, title and year separated by tabs for the // given line of the input file public static void print(String line) { Scanner data = new Scanner(line); double rating = data.nextDouble(); int year = data.nextInt(); int rank = data.nextInt(); System.out.print(rank + "\t" + rating + "\t"); while (data.hasNext()) { System.out.print(data.next() + " "); } System.out.println("(" + year + ")"); } }
Stuart Reges
Last modified: Mon May 11 08:22:54 PDT 2020