// This program reads a file of IMDB's Top 250 movies and // displays information about movies that match a search // string typed by the user. // // Text-only version. import java.awt.*; import java.io.*; import java.util.*; public class Movies { public static void main(String[] args) throws FileNotFoundException { introduction(); String phrase = getWord(); Scanner input = new Scanner(new File("imdb.txt")); search(input, phrase); } // prints introductory text to the user public static void introduction() { System.out.println("This program will allow you to search the"); System.out.println("imdb top 250 movies for a particular word."); System.out.println(); } // Asks the user for their search phrase and returns it. public static String getWord() { System.out.print("Search word: "); Scanner console = new Scanner(System.in); String phrase = console.next(); phrase = phrase.toLowerCase(); System.out.println(); return phrase; } // Breaks apart each line, looking for lines that match the search phrase. // Prints information about each movie that matches the phrase. // // example line: "2 8.9 113807 The Lord of the Rings: The Return of the King (2003)" public static void search(Scanner input, String phrase) { System.out.println("Rank\tVotes\tRating\tTitle"); int matches = 0; while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int rank = lineScan.nextInt(); int votes = lineScan.nextInt(); double rating = lineScan.nextDouble(); String title = lineScan.nextLine(); // all the rest String lcTitle = title.toLowerCase(); if (lcTitle.indexOf(phrase) >= 0) { matches++; System.out.println(rank + "\t" + votes + "\t" + rating + title); } } System.out.println(); System.out.println(matches + " matches."); } }