printRating

Category: Token-Based File Processing
Author: Benson Limketkai
Book Chapter: 6.3
Problem: printRating
(a) (11 points) Write a method named printRating that takes a string containing movie information as a parameter and prints out a summary of that information.

The string parameter will be formatted as follows:

      X 

For example, all of the following are proper examples of possible strings that could be passed to printRating:

     7.0 7.3 8.0 8.0 8.2 7.2 X The Hangover Part II
     8.2 8.5 8.9 8.9 X X-Men: First Class
     7.5 8.3 7.4 6.9 8.7 7.9 X Bridesmaids
     7.4 X Thor

Assume that every movie has at least one rating.

The summarized information will be formatted as follows:

     :  ( ratings)

The method also returns the number of ratings in the string.

Examples:

      Method call: printRating("7.0 7.3 8.0 8.0 8.2 7.2 X The Hangover Part II");
      Output: The Hangover Part II: 7.616666666666667 (6 ratings)
      Returns: 6

      Method call: printRating("8.2 8.5 8.9 8.9 X X-Men: First Class");
      Output: X-Men: First Class: 8.625 (4 ratings)
      Returns: 4

      Method call: printRating("7.5 8.3 7.4 6.9 8.7 7.9 X Bridesmaids");
      Output: Bridesmaids: 7.783333333333332 (6 ratings)
      Returns: 6

      Method call: printRating("7.4 X Thor");
      Output: Thor: 7.4 (1 ratings)
      Returns: 1

(b) (7 points) Using printRating, write a method named printMovieFile that takes a Scanner for an input file as a parameter. Each line of the input file contains movie information as specified in part (a) of this problem. The method should output the summarized information and end with the total number of ratings.

For example, if the input file movies.txt contained the following:

     7.0 7.3 8.0 8.0 8.2 7.2 X The Hangover Part II
     8.2 8.5 8.9 8.9 X X-Men: First Class
     7.5 8.3 7.4 6.9 8.7 7.9 X Bridesmaids
     7.4 X Thor

Then the call printMovieFile(new Scanner(new File("movies.txt"))) would output

      The Hangover Part II: 7.616666666666667 (6 ratings)
      X-Men: First Class: 8.625 (4 ratings)
      Bridesmaids: 7.783333333333332 (6 ratings)
      Thor: 7.4 (1 ratings)
     
     Total ratings: 17