// Helene Martin, CSE 142 // Pronmpts the user for a search word and displays all movies // from the IMDB top 250 movies that match that word, case-insensitively. import java.io.*; import java.util.*; public class IMDBSearcherv2 { public static void main(String[] args) throws FileNotFoundException { printIntro(); Scanner console = new Scanner(System.in); System.out.print("Search word: "); String searchWord = console.next(); int matches = 0; Scanner fileScan = new Scanner(new File("imdb.txt")); String dataLine = find(searchWord, fileScan); if (dataLine.length() > 0) { System.out.println("Rank Votes Rating Title"); while (dataLine.length() > 0) { printMovie(dataLine); matches++; dataLine = find(searchWord, fileScan); } } System.out.println(matches + " matches."); } // Prints the introduction. public static void printIntro() { 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(String searchWord, Scanner fileScan) { while (fileScan.hasNextLine()) { String dataLine = fileScan.nextLine(); String dataLineLC = dataLine.toLowerCase(); if (dataLineLC.contains(searchWord.toLowerCase())) { return dataLine; } } return ""; } // Formats a movie's data for output. // Note: we need to use a Scanner for the line because the output // has data in a different order than the input public static void printMovie(String dataLine) { Scanner lineScan = new Scanner(dataLine); int ranking = lineScan.nextInt(); double rating = lineScan.nextDouble(); int votes = lineScan.nextInt(); String title = ""; while (lineScan.hasNext()) { title += lineScan.next() + " "; } System.out.println(ranking + "\t" + votes + "\t" + rating + "\t" + title); } }