// Marty Stepp, CSE 142, Spring 2010 // This program reads a file containing the IMDb top 250 all-time movies // and displays all movies matching a search phrase. // // This first version has everything in main, with no methods. // It would not get full points for internal correctness! // // input: // 1 9.1 243,153 The Godfather (1972) // // output: // Search word? part // Rank Votes Rating Title // 2 139085 9.0 The Godfather: Part II (1974) // 40 129172 8.5 The Departed (2006) // 95 20401 8.2 The Apartment (1960) // 192 30587 8.0 Spartacus (1960) // 4 matches. import java.io.*; // for File import java.util.*; // for Scanner public class Movies { public static void main(String[] args) throws FileNotFoundException { String searchWord = getWord(); Scanner input = new Scanner(new File("imdb.txt")); String line = search(input, searchWord); int matches = 0; if (line.length() > 0) { System.out.println("Rank\tVotes\tRating\tTitle"); while (line.length() > 0) { matches++; display(line); line = search(input, searchWord); } } System.out.println(matches + " matches."); } // Asks the user for their search word and returns it. public static String getWord() { System.out.print("Search word: "); Scanner console = new Scanner(System.in); String searchWord = console.next(); searchWord = searchWord.toLowerCase(); System.out.println(); return searchWord; } // Breaks apart each line, looking for lines that match the search word. public static String search(Scanner input, String searchWord) { while (input.hasNextLine()) { String line = input.nextLine(); String lineLC = line.toLowerCase(); // case-insensitive match if (lineLC.indexOf(searchWord) >= 0) { return line; } } return ""; // not found } // Displays the line in the proper format on the screen. 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()) { title += lineScan.next() + " "; // the rest of the line } System.out.println(rank + "\t" + votes + "\t" + rating + "\t" + title); } }