is there a simpler way of doing this??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • outofmymind
    New Member
    • Oct 2006
    • 45

    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:

    Code:
    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();
    		}
    	
    		
    		
    	}
    }
    Thanks!!
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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.
    Code:
                            while((s=br.readLine())!=null)
                            {
                            StringTokenizer t = new StringTokenizer(s," ");
                                    a =Integer.parseInt(t.nextToken());
                                    b = Float.parseFloat(t.nextToken());
                                     sum = sum+b;
                                     count++;
                           }
    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

    Comment

    • Soujiro
      New Member
      • Jan 2007
      • 35

      #3
      Thats quite simple already..

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by outofmymind
        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:

        Code:
        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();
        		}
         
         
         
        	}
        }
        Thanks!!
        Realise alos that calling
        "20311 15".split(" "); returns the array {"20311", "15"} .

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          I read somewhere that there is always a simpler way.....

          Comment

          Working...