Okay, here is my problem 7) A spreadsheet file can be stored as a comma separate
ID: 3696923 • Letter: O
Question
Okay, here is my problem
7) 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.
I don't have any more information other than this. It is not a continuation of a previous problem or anything. You need to use eclipse for this problem, not excel.
Thanks!
Explanation / Answer
import java.io.*;
import java.util.*;
class FileRead
{
public static void main(String args[]) throws Exception
{
BufferedWriter output = null;
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("data.csv");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// BufferedWriter output = null;
String strLine;
ArrayList<String> arrayList = new ArrayList<>();
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
arrayList.add(strLine);
}
//Close the input stream
in.close();
File file = new File("example.txt");
output = new BufferedWriter(new FileWriter(file));
int i=0;
int size= arrayList.size();
String lastString = arrayList.get(size - 1);
String totalMarks[] = lastString.split(",");
int tMarks = 0;
for(int j=1;j<totalMarks.length;j++){
tMarks += Integer.parseInt(totalMarks[j]);
}
String strTokens[] = null;
for(String str: arrayList){
if(i==0){
output.write(str+",finalpercentage ");
// System.out.println(str+",final percentage");
} else if(i==size){
output.write(str+",100 ");
// System.out.println(str + ",100");
} else{
strTokens = str.split(",");
int stTMarks = 0;
for(int j=1;j<strTokens.length;j++){
stTMarks += Integer.parseInt(strTokens[j]);
}
output.write(str + "," + (stTMarks * 100) / tMarks + " ");
// System.out.println(str + "," + (stTMarks * 100) / tMarks+" ");
}
i++;
}
}catch (Exception e){//Catch exception if any
e.printStackTrace(System.out);
}finally {
if ( output != null ) {
output.close();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.