// Helene Martin, CSE 142 // Displays IMDB's Top 250 movies that match a search string. // goals: illustrate chaining and an undescriptive main. // if your program looks like this, you should be able to restructure it. import java.io.*; import java.util.*; public class BadIMDBSearcher { public static void main(String[] args) throws FileNotFoundException { getWord(); } // Asks the user for their search word. public static void getWord() throws FileNotFoundException { System.out.print("Search word: "); Scanner console = new Scanner(System.in); String searchWord = console.next(); searchWord = searchWord.toLowerCase(); System.out.println(); Scanner input = new Scanner(new File("imdb.txt")); search(input, searchWord); } // Breaks apart each line, looking for lines that match the search word. public static void search(Scanner input, String searchWord) { int matches = 0; while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int rank = lineScan.nextInt(); double rating = lineScan.nextDouble(); int votes = lineScan.nextInt(); String title = lineScan.nextLine(); // all the rest if (title.toLowerCase().indexOf(searchWord) >= 0) { matches++; System.out.println("Rank\tVotes\tRating\tTitle"); display(line); } } System.out.println(matches + " matches."); } // 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); } }