// Allison Obourn, CSE 142 // Displays IMDB's Top 250 movies that match a search string. import java.util.*; import java.io.*; public class Imdb { public static void main(String[] args) throws FileNotFoundException { String word = getWord(); Scanner input = new Scanner(new File("imdb.txt")); String line = search(input, word); if (!line.equals("")) { System.out.println("Rank\tVotes\tRating\tTitle"); int matches = 0; while(!line.equals("")) { display(line); line = search(input, word); matches++; } System.out.println(matches + " matches"); } } // Prompts the user for a word and retruns their response public static String getWord() { Scanner console = new Scanner(System.in); System.out.print("Search word? "); String word = console.next(); word = word.toLowerCase(); return word; } // Finds and returns then next line matching the search phrase public static String search(Scanner input, String word) { while(input.hasNextLine()) { String line = input.nextLine(); String lineLC = line.toLowerCase(); if(lineLC.indexOf(word) >= 0) { return line; } } return ""; } // Displays tab-separated information about a movie // 1 9.1 243153 The Godfather (1972) public static void display(String line) { Scanner lineScan = new Scanner(line); int rank = lineScan.nextInt(); double rating = lineScan.nextDouble(); int votes = lineScan.nextInt(); String title = ""; while (lineScan.hasNext()) { // cumulative sums are for Strings, too title += lineScan.next() + " "; } System.out.println(rank + "\t" + votes + "\t" + rating + "\t" + title); } }