// Tyler Rigsby, CSE 142 // Searches through the IMDb top 250 movies given a search phrase and // displays the matches import java.util.*; import java.io.*; public class Movies { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); Scanner input = new Scanner(new File("imdb.txt")); PrintStream output = new PrintStream(new File("results.txt")); giveIntro(); System.out.print("Enter search phrase: "); String search = console.nextLine().toLowerCase(); String result = find(input, search); int count = 0; if (result.length() > 0) { output.println("Rank\tRating\tTitle"); while (result.length() > 0) { print(result, output); result = find(input, search); count++; } } System.out.println(count + " matches found."); } // Prints the film in the result String formatted with tabs // to the output PrintStream public static void print(String result, PrintStream output) { Scanner lineScan = new Scanner(result); int ranking = lineScan.nextInt(); int votes = lineScan.nextInt(); double rating = lineScan.nextDouble(); String title = ""; while (lineScan.hasNext()) { title = title + lineScan.next() + " "; } output.println(ranking + "\t" + rating + "\t" + title); } // Searches the input Scanner for lines containing the search // String, and returns the next occurrence, or the empty String // if none is found. public static String find(Scanner input, String search) { while (input.hasNextLine()) { String line = input.nextLine(); if (line.toLowerCase().contains(search)) { return line; } } return ""; } // 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(); } }