// [This version is better style (no chaining) and produces text output.] // Displays IMDB's Top 250 movies that match a search string. import java.io.*; // for File import java.util.*; // for Scanner public class MoviesTextOutput { public static String MOVIE_FILE = "imdb.txt"; public static void main(String[] args) throws FileNotFoundException { String searchWord = getWord(); Scanner input = new Scanner(new File(MOVIE_FILE)); String line = search(input, searchWord); int matches = 0; if (line.length() > 0) { System.out.println("#\tRating\tVotes\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(); int votes = lineScan.nextInt(); double rating = lineScan.nextDouble(); String title = ""; while (lineScan.hasNext()) { title += lineScan.next() + " "; // the rest of the line } System.out.println(rank + "\t" + rating + "\t" + votes + "\t" + title); } }