I am doing a first year level JAVA subject and am having problems writing a program. The aim is to implement a program that performs statistical calculations on students records from a text file. The input text file looks as follows:
The first word being the student name, the second being a subject code, followed by a grade, followed by another subject code, followed by a grade, etc.
The program must contain one line for each course in the input, containing the course name, the average course grade and the number of students taking the subject. The output must also contain two lines with the mean and the standard deviation of the course averages. Everything must be rounded to two decimal places. Example output as follows:
I know that basically I have to write some code with BufferedReader, Printwriter, StringTokenizer, and stuff but I just don't really know where to start or what to do. If anyone could help me I would really appreciate it.
I hope this helps...you need to first write some thing that reads a file and displays it..
import java.io.*;
class readFile(){ public static void main(String [] args) throws IOEXception{ BufferedReader fin = new BufferedReader(new FileReader("x.txt"));
PrintWriter fout = new PrintWriter(new FileWriter("y.txt"));
String s;
for (int i=0;;i++){ s = fin.readLine(); if (s==null) break; else fout.println(i + " " + s);} fout.close(); } } just play around with this so that you can also read in integers, etc.
Hi, this is an example class for one subject (comp1). It could be something based on this for more subjects.I define two static variables, comp1 and grade1 to take account for the number of students following comp1 course, and for the average course grade.
import java.io.*; import java.util.*;
class Calculator{
public static int comp1; public static int grade1;
public static void main(String[] args){ try{ try{ //read from file "students.txt" BufferedReader br = new BufferedReader(new FileReader("students.txt")); //write in file "subjects.txt" PrintWriter pw = new PrintWriter(new FileWriter("subjects.txt"));
String s;
while ((s= br.readLine())!=null){ StringTokenizer st = new StringTokenizer(s); while(st.hasMoreElements()){ if (st.nextToken().equals("comp1")){ if(st.hasMoreElements()) grade1 += Integer.decode(st.nextToken()).intValue(); comp1++; }
} } //we get mean value: If included to avoid division by zero exception when compiling if(comp1!=0) grade1 = grade1/comp1; //write results in output file pw.println("comp1 "+grade1 + " " +comp1); pw.close(); } catch (FileNotFoundException nfe){}