473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

is there a simpler way of doing this??

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
4 1179
horace1
1,510 Expert 1GB
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
35
Thats quite simple already..
Jan 10 '07 #3
r035198x
13,262 8TB
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
1,806 1GB
I read somewhere that there is always a simpler way.....
Jan 10 '07 #5

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

Similar topics

4
by: Kerim Borchaev | last post by:
Hello! Always when I use "super" I create a code duplication because class used as first arg to "super" is always the class where the method containing "super" was defined in: ''' class C:...
4
by: Robin Bryce | last post by:
Hi, I've been looking at generators from the context of event oriented web application development. I was thinking of submitting a version of http://www.wiretooth.com/eventhub_recipe.html as a...
8
by: Burgess Meredith | last post by:
I suffered through the interminal drivel, that the Dynamic Dummies, freeagent-Man, and the Boy Blunder had to say, > On Thu, 09 Oct 2003 11:57:07 +1000, Chris wrote: > >> On Thu, 09 Oct 2003...
2
by: Alessandro Bottoni | last post by:
I just tried to use the python standard logging module in a small program and I found myself lost among all those features, all those configuration options and so on. Is there any better/simpler...
2
by: Greg Strong | last post by:
Hello All, Is there a simpler way to count text boxes with data in them when the 4 text boxes are in the header of a form? I've written the code below and it works. ,----- | Private Sub...
2
by: meh | last post by:
I'm not sure if I have the right concept going. I am making a custom TabPage and executing it from a button click event. It works fine I'm just thinking I might be missing the finer points of the...
1
by: Roy Gourgi | last post by:
Hi, Here is a much simpler version of my time scheduling program. As you can see in the GV (Global Variable class) I have 2 arrays gaSumWorkDays (already initialized with the values) and ...
17
by: ZorpiedoMan | last post by:
Why Doesn't THIS work: ----------------------------------------------------------- A Windows Form has three controls: TextBox1 BindButton ShowTextButton ChangeTextButton Code:
36
by: phil-news-nospam | last post by:
Here is a simpler (no drop shadows) example of the padding bug I see: http://phil.ipal.org/usenet/ciwas/2006-05-08/buttons-1.html So far I find nothing in the CSS2 document that says I should...
6
by: mcse jung | last post by:
Here is asample program that writes a program and then executes it. Do you knowof a much simpler way of writing a program that writes a program? """...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.