I'd like to know if theres a "shorter" way to solving this problem, oh and btw, i keep gtting a exception :(
here's the que:
Student number and Marks of all students in final of subject CSCI213 are written into a text file “exam.in”. (5 marks)
Write an application to do the following tasks:
•Find the average of marks of all students in the class
•Display the subject name as “CSCI213”, component name as “Class Test2” and the class average calculated by your program on the screen.
Sample Input: exam.in
67868 18
08552 11.5
49966 14
67169 15
78677 15
16998 13
44345 15
80324 10
40238 13.5
13973 8.5
83070 7.5
64165 9
76977 16
64350 16
64049 9
48367 7.5
65133 9.5
84999 11
59103 5.5
34393 9
87483 11
49098 10
11897 10.5
05149 13
48896 9.5
49733 6
65017 6.5
26783 9
92202 5
64153 10
20311 15
Expected Output: exam.out
CSCI213 Class Test 2 11
here's my work:
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import java.util.StringTokenizer;
- public class 213Final{
- public static void main(String[]args)
- {
- int count=0;
- float sum=0;
- float avg=0;
- String s = new String();
- try
- {
- FileReader fr = new FileReader("test.txt");
- BufferedReader br = new BufferedReader (fr);
- StringTokenizer t = new StringTokenizer(s," ");
- int a;
- float b;
- while((s=br.readLine())!=null)
- {
- a =Integer.parseInt(t.nextToken());
- b = Float.parseFloat(t.nextToken());
- sum = sum+b;
- count++;
- //System.out.println(s);
- }
- }
- catch(EOFException g)
- {
- avg = sum/count;
- g.printStackTrace();
- }
- catch (FileNotFoundException fn)
- {
- fn.printStackTrace();
- }
- catch (IOException e)
- {
- System.out.println(e);
- }
- try
- {
- FileWriter fw= new FileWriter("exam.out");
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write("CSCI213 "+"" +"finalExam "+avg);
- bw.flush();
- }
- catch(IOException q)
- {
- q.printStackTrace();
- }
- }
- }