By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,558 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,558 IT Pros & Developers. It's quick & easy.

is there a simpler way of doing this??

P: 45
Ok this is the last question for today,

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
  1. import java.io.*;
  2. import java.util.StringTokenizer;
  3.  
  4. public class 213Final{
  5.     public static void main(String[]args)
  6.     {
  7.         int count=0;
  8.         float sum=0;
  9.         float avg=0;
  10.         String s = new String();
  11.  
  12.         try
  13.         {
  14.             FileReader fr = new FileReader("test.txt");
  15.             BufferedReader br = new BufferedReader (fr);
  16.  
  17.             StringTokenizer t = new StringTokenizer(s," ");
  18.  
  19.             int a;
  20.             float b;
  21.  
  22.             while((s=br.readLine())!=null)
  23.             {
  24.                 a =Integer.parseInt(t.nextToken());
  25.                 b = Float.parseFloat(t.nextToken());
  26.                  sum = sum+b;
  27.                  count++;
  28.  
  29.                 //System.out.println(s);
  30.             }
  31.  
  32.         }
  33.  
  34.         catch(EOFException g)
  35.         {
  36.              avg = sum/count;
  37.             g.printStackTrace();
  38.         } 
  39.  
  40.         catch (FileNotFoundException fn)
  41.         {
  42.             fn.printStackTrace();
  43.         }
  44.  
  45.         catch (IOException e)
  46.         {
  47.             System.out.println(e);
  48.         }
  49.  
  50.         try
  51.         {
  52.         FileWriter fw= new FileWriter("exam.out");
  53.         BufferedWriter bw = new BufferedWriter(fw);
  54.  
  55.         bw.write("CSCI213    "+"" +"finalExam  "+avg);
  56.         bw.flush();
  57.         }
  58.  
  59.         catch(IOException q)
  60.         {
  61.             q.printStackTrace();
  62.         }
  63.  
  64.  
  65.  
  66.     }
  67. }
Thanks!!
Jan 8 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
looks a reasonable way of calculating the average by forming a sum as you read the data and dividing by the number of records read. Note your StringTokenizer should be inside the loop where you read the data, e.g.
Expand|Select|Wrap|Line Numbers
  1.                         while((s=br.readLine())!=null)
  2.                         {
  3.                         StringTokenizer t = new StringTokenizer(s," ");
  4.                                 a =Integer.parseInt(t.nextToken());
  5.                                 b = Float.parseFloat(t.nextToken());
  6.                                  sum = sum+b;
  7.                                  count++;
  8.                        }
  9.  
as an alternative to using a StringTokenizer have a look at Scanner for reading data
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
Jan 8 '07 #2

Soujiro
P: 35
Thats quite simple already..
Jan 10 '07 #3

10K+
P: 13,264
Ok this is the last question for today,

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
  1. import java.io.*;
  2. import java.util.StringTokenizer;
  3.  
  4. public class 213Final{
  5.     public static void main(String[]args)
  6.     {
  7.         int count=0;
  8.         float sum=0;
  9.         float avg=0;
  10.         String s = new String();
  11.  
  12.         try
  13.         {
  14.             FileReader fr = new FileReader("test.txt");
  15.             BufferedReader br = new BufferedReader (fr);
  16.  
  17.             StringTokenizer t = new StringTokenizer(s," ");
  18.  
  19.             int a;
  20.             float b;
  21.  
  22.             while((s=br.readLine())!=null)
  23.             {
  24.                 a =Integer.parseInt(t.nextToken());
  25.                 b = Float.parseFloat(t.nextToken());
  26.                  sum = sum+b;
  27.                  count++;
  28.  
  29.                 //System.out.println(s);
  30.             }
  31.  
  32.         }
  33.  
  34.         catch(EOFException g)
  35.         {
  36.              avg = sum/count;
  37.             g.printStackTrace();
  38.         } 
  39.  
  40.         catch (FileNotFoundException fn)
  41.         {
  42.             fn.printStackTrace();
  43.         }
  44.  
  45.         catch (IOException e)
  46.         {
  47.             System.out.println(e);
  48.         }
  49.  
  50.         try
  51.         {
  52.         FileWriter fw= new FileWriter("exam.out");
  53.         BufferedWriter bw = new BufferedWriter(fw);
  54.  
  55.         bw.write("CSCI213 "+"" +"finalExam "+avg);
  56.         bw.flush();
  57.         }
  58.  
  59.         catch(IOException q)
  60.         {
  61.             q.printStackTrace();
  62.         }
  63.  
  64.  
  65.  
  66.     }
  67. }
Thanks!!
Realise alos that calling
"20311 15".split(" "); returns the array {"20311", "15"} .
Jan 10 '07 #4

DeMan
100+
P: 1,806
I read somewhere that there is always a simpler way.....
Jan 10 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.