processSets
Category: Token-Based File Processing
Author: Stuart Reges
Book Chapter: 6.2
Problem: processSets
Write a static method called
processSets that takes a Scanner as a parameter and that prints information
about a series of data sets stored in the Scanner. The Scanner will contain
a sequence of data entries where an entry is either:
- a positive integer followed by a real number, which indicates that
the given real number occurs the given number of times in the data
set, as in "3 4.2" which would indicate 3 occurrences of 4.2
- a 0, which indicates the end of the data set
For example, suppose the Scanner contains the following data:
3 4.5 2 9.8 7 4.5 0 4 2.7 5 3.9 0 2 3.2 4 7.1 0
This input includes three data sets because it has three zeros. The first
data set has 3 occurrences of 4.5, 2 occurrences of 9.8 and 7 occurrences of
4.5 (terminated by a 0). The second data set has 4 occurrences of 2.7 and 5
occurrences of 3.9, again terminated by a 0. The third data set has 2
occurrences of 3.2 and 4 occurrences of 7.1 terminated by a 0.
Your method should report for each data set the total number of entries in
the data set and the average of the numbers. For the input above, the
following output should be produced:
total values = 12
average value = 5.383333333333333
total values = 9
average value = 3.3666666666666667
total values = 6
average value = 5.8
You must exactly reproduce this format. The value 0 that is used to
terminate each data set should not be included in the computations. Notice
that you have to account for the multiple occurrences of each number in
reporting the total number of values and the overall average for the data
set. For example, the first data set is specified by three pairs of
numbers, but because of the duplicates, it contains a total of 12 numbers
and the overall average of those 12 numbers is reported.
If the Scanner is initially empty, your method should produce no output.
Otherwise you may assume that no data set is empty and that each data set is
terminated by a 0 (including the last data set).
Write your solution to processSets below.