SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes I
Exercise:
Programming Projects
Question:14 | ISBN:9780132830317 | Edition: 5

Question

A comma-separated values (CSV) file is a simple text format used to store a list of records. A comma is used as a delimiter to separate the fields for each record. This format is commonly used to transfer data between a spreadsheet or database. In this Programming Project, consider a store that sells five products abbreviated as A, B, C, D, and E. Customers can rate each product from 1–5, where 1 is poor and 5 is excellent. The ratings are stored in a CSV file where each row contains the customer’s rating for each product. Here is a sample file with three customer ratings:

In this file format, the first line gives the products. The digit 0 indicates that a customer did not rate a product. In this case, the first customer rated A as 3, C as 5, D as 1, and E as 2. Product B was not rated. The third customer rated C as 5, D as 1, and E as 3. The third customer did not rate A or B.

Create a text file in this format with sample ratings. Then, write a program that reads in this text file and extracts each rating using the StringTokenizer class. Finally, the program should output the average rating for each product. Customers that did not rate a product should not be considered when computing the average rating for that product. Your program can assume there will always be exactly five products but it should work with an arbitrary number of customer ratings.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class ProductRateDemo 
{
	public static void main(String[] args) 
	{

		Scanner fr = null;
		DecimalFormat df = new DecimalFormat("0.00");
		try {

			fr = new Scanner(new FileInputStream("customerRatings.txt"));
		} catch (FileNotFoundException e) {

			System.out.println("File not found");
			System.exit(0);
		}
		String productRates = fr.nextLine();
		StringTokenizer splitproductRates = new StringTokenizer(productRates, ",");
		int productRatesCount = splitproductRates.countTokens();
		String[] productRatesArray = new String[productRatesCount];
		double[] total = new double[productRatesCount];
		int[] countProduct = new int[productRatesCount];
		int mark = 0;
		while (splitproductRates.hasMoreTokens()) {
			String product = splitproductRates.nextToken();
			productRatesArray[mark] = product;
			mark = mark + 1;
		}
		int rowsCount = 50;
		int rows = 0;
		int columns = 0;
		int[][] ratingsarray = new int[rowsCount][productRatesCount];
		int userRate;
		while (fr.hasNextLine()) {
			String ratingsLine = fr.nextLine();
			StringTokenizer splitRatings = new StringTokenizer(ratingsLine, ",");
			while (splitRatings.hasMoreElements()) {
				String prodcutARating = splitRatings.nextToken();
				userRate = prodcutARating.charAt(0) - 48;
				if (userRate > 0) 
				{
					ratingsarray[rows][columns] = userRate;
				}

				columns++;
			} 

			rows++;
			columns = 0;
		}
		fr.close();

		int x = rows;
		int y = productRatesCount;
		System.out.println("User Ratings from input file:customerRatings.txt");
		for (int i = 0; i < x; i++)
		{
			for (int j = 0; j < y; j++) {
				System.out.print(ratingsarray[i][j] + " ");
			}
			System.out.println();
		}

		for (int columns1 = 0; columns1 < y; columns1++) {
			for (int rows1 = 0; rows1 < x; rows1++) {
				total[columns1] += ratingsarray[rows1][columns1];
				if (ratingsarray[rows1][columns1] > 0)
					countProduct[columns1] = countProduct[columns1] + 1;
			}
		}
		System.out.println("\n***CUSTOMER PRDOUCT RATINGS***\n");
		
		for (int t = 0; t < productRatesCount; t++) {
			System.out.println("Product " + productRatesArray[t] + " average rate: "
					+ df.format(total[t] / countProduct[t]));
		}
	}
}

Input file data(customerRatings.txt):

A,B,C,D,E
3,0,5,1,2
1,1,4,2,1
0,0,5,1,3


OUTPUT:

User Ratings from input file:customerRatings.txt
3 0 5 1 2 
1 1 4 2 1 
0 0 5 1 3 

***CUSTOMER PRODUCT RATINGS***

Product A average rate: 2.00
Product B average rate: 1.00
Product C average rate: 4.67
Product D average rate: 1.33
Product E average rate: 2.00

 

0 0

Discussions

Post the discussion to improve the above solution.