Hello, I am a newbie to Java and I need to answer this 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:
A,B,C,D,E
3,0,5,1,2
1,1,4,2,1
0,0,5,1,3
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.
My answer in Java is:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class StringTokenizer
{
int A;
int B;
int C;
int D;
int E;
public static void main(String [] args)
{
String line;
String ratings[];
StringTokenizer sT[]=new StringTokenizer[50];
String fileName;
Scanner input=new Scanner(System.in);
try
{
System.out.println("enter the text file name that contains cutomer rating records(ex- record.txt)");
fileName=input.nextLine();
FileReader file=new FileReader(fileName);
BufferedReader f = new BufferedReader(file);
int i=0;
while((line=f.readLine())!=null)
{
line=line.trim();
ratings=line.split(",");
sT[i]=new StringTokenizer();
sT[i].A=Integer.parseInt(ratings[0]);
sT[i].B=Integer.parseInt(ratings[1]);
sT[i].C=Integer.parseInt(ratings[2]);
sT[i].D=Integer.parseInt(ratings[3]);
sT[i].E=Integer.parseInt(ratings[4]);
i++;
}
float avgA=0;
float avgB=0;
float avgC=0;
float avgD=0;
float avgE=0;
System.out.println("customer rating records:");
System.out.println("A\tB\tC\tD\tE");
for(int j=0;j<i;j++)
{
System.out.println(sT[j].A+"\t"+sT[j].B+"\t"+sT[j].C+"\t"+sT[j].D+"\t"+sT[j].E+"\t");
avgA=avgA+sT[j].A;
avgB=avgB+sT[j].B;
avgC=avgC+sT[j].C;
avgD=avgD+sT[j].D;
avgE=avgE+sT[j].E;
}
System.out.println("average rating of A= "+avgA/i);
System.out.println("average rating of B= "+avgB/i);
System.out.println("average rating of C= "+avgC/i);
System.out.println("average rating of D= "+avgD/i);
System.out.println("average rating of E= "+avgE/i);
}catch(FileNotFoundException ex)
{
}
catch(IOException ex)
{
}
}
}
I have the txt file in the same folder as the java project. But, I am keep getting this ERROR!!
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at StringTokenizer.main(StringTokenizer.java:68)
And line 68 is:
PLEASE HELP! ='(