IN JAVA LANGUGE: A spreadsheet file can be stored as a comma separated file. For
ID: 3770940 • Letter: I
Question
IN JAVA LANGUGE:
A spreadsheet file can be stored as a comma separated file. For instance if a file called data.csv had the following in it:
name, quiz1, quiz2, quiz3, hw1, hw2, hw3, midterm, final
bill,8,9,10,50,45,43,88,93
jill,9,9,10,50,48,40,90,91
gill,6,5,7,38,22,40,70,73
will,8,9,9,40,45,43,83,94
totals,10,10,10,50,50,50,100,100
It would display in a program like Microsoft excel appropriately.
a) create a file called data.csv with this data on your hard drive.
b) read the file into a program calculating each students final percentage.
c) have the program write a new file with an extra column containing the final percentage for each student, a column heading for the first row, and the last row can end with 100 for its percentage.
Explanation / Answer
package javaprogram;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
class Student
{
public String name;
public int quiz1,quiz2,quiz3;
public int hw1,hw2,hw3;
public int midterm,finalExam ;
public float finalPercentage;
Student(String name, int h1, int h2, int h3,int q1, int q2, int q3, int mt, int fe,int SumOftotals)
{
this.name=name;
this.hw1=h1;
this.hw2=h2;
this.hw3=h3;
this.quiz1=q1;
this.quiz2=q2;
this.quiz3=q3;
this.midterm=mt;
this.finalExam=fe;
this.finalPercentage=(hw1+hw2+hw3+quiz1+quiz2+quiz3+midterm+finalExam)/(SumOftotals);
}
}
public class JavaProgram {
public static void main(String[] args) {
try {
String csvFile = "data.csv";
Student students[]=new Student[10];
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line = "",lines[]=new String[10];
StringTokenizer st = null;
int lineNumber=0,i;
//read comma separated file line by line
while ((line = br.readLine()) != null)
{
//use comma as token separator
lines[lineNumber++]=line;
}
int total[]=new int[9],SumOfTotals=0,count=0;
st = new StringTokenizer(lines[lineNumber-1], ",");
while(st.hasMoreTokens())
{
total[count]=Integer.parseInt(st.nextToken());
SumOfTotals+=total[count];
count++;
}
total[count]=100;
SumOfTotals+=100;
String heading[]=new String[9];
int hcount=0;
st = new StringTokenizer(lines[0], ",");
while(st.hasMoreTokens())
{
heading[count]=st.nextToken();
hcount++;
}
heading[hcount]="finalPercentage";
for(i=1;i<lineNumber-1;i++)
{
st = new StringTokenizer(lines[i], ",");
String name=st.nextToken();
int h1=Integer.parseInt(st.nextToken());
int h2=Integer.parseInt(st.nextToken());
int h3=Integer.parseInt(st.nextToken());
int q1=Integer.parseInt(st.nextToken());
int q2=Integer.parseInt(st.nextToken());
int q3=Integer.parseInt(st.nextToken());
int mt=Integer.parseInt(st.nextToken());
int fe=Integer.parseInt(st.nextToken());
students[i]=new Student(name,h1,h2,h3,q1,q2,q3,mt,fe,SumOfTotals);
}
}
// writing into new CSV
FileWriter writer = new FileWriter("out.csv");
writer.append("DisplayName");
writer.append(',');
writer.append("Age");
writer.append(' ');
writer.append("MKYONG");
writer.append(',');
writer.append("26");
writer.append(' ');
writer.append("YOUR NAME");
writer.append(',');
writer.append("29");
writer.append(' ');
//generate whatever data you want
writer.flush();
writer.close();
}
} catch (Exception e) {
System.err.println("CSV file cannot be read : " + e);
}
}
}