reading data into vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • syncjc822
    New Member
    • Nov 2006
    • 3

    #1

    reading data into vectors

    hi,I am working on a program that reads data from a text file into a vector and computes the average score, and highest, lowest score...here is what I have started..




    Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class ProcessScoresV1{
    
    	public static void main(String[] args)throws FileNotFoundException {
    
    		Scanner infile = new Scanner(new FileReader("scores.txt"));
    
    		int count = 0;
    				while (infile.hasNext()) {
    					String word = infile.next();
    						count++;
    				}
    
    		final long SENTINEL = -999;
    		double averageTotal = 0.0;
    		double averageLow = 0.0;
    		double averageHigh =0.0;
    
    
    		while (infile.hasNext()) {
    
    					double scoreTotal = 0;
    					long scoreCount = 0;
    					double score;
    
    		while (infile.hasNextDouble()) {
    				score = infile.nextDouble();
    				scoreTotal += score;
    				scoreCount++;
    
    
    			double average = scoreTotal/scoreCount;           // computes average of all scores
    			averageTotal += average;
    
    							//high score variables
    				double scorehigh;
    				long scoreCounthigh = 0;
    				double scoreTotalhigh = 0;
    
    				// low score variables
    				double scorelow;
    				long scoreCountlw = 0;
    				double scoreTotallw = 0;
    
    
    			if (score < 70) {                    //condition statement to compute low scores
    				scorelow = infile.nextDouble();
    				scoreTotallw += scorelow;
    				scoreCountlw++;
    
    			double averageL = scoreTotallw/scoreCountlw;  // computes averages of low scores
    			averageLow += averageL;
    
    
    			if (score > 70) {                  //condition statement to compute high scores
    		      scorehigh = infile.nextDouble();
    		      scoreTotalhigh+= scorehigh;
    		      scoreCounthigh++;
    
    
    		    double averageH = scoreTotalhigh/scoreCounthigh; // computes averages of high scores
    			averageH += averageHigh;
    
    			JOptionPane.showMessageDialog(null, "Score Total" +  scoreTotal + "Total Average Score" + average);
    			JOptionPane.showMessageDialog(null, "High scores" +  scoreTotalhigh + "Average High Scores" + averageH);
    			JOptionPane.showMessageDialog(null, "Low scores" +  scoreTotallw + "Average Low Scores" + averageL);
    			JOptionPane.showMessageDialog(null, "Number of Total Scores" + count);
    
    
    }
    }
    }
    }
    }
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by syncjc822
    hi,I am working on a program that reads data from a text file into a vector and computes the average score, and highest, lowest score...here is what I have started..
    What's your question?

    Comment

    • syncjc822
      New Member
      • Nov 2006
      • 3

      #3
      im not sure how to go about adding vectors in my program

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by syncjc822
        im not sure how to go about adding vectors in my program
        Since you know how to read a file, just create a vector using
        Code:
         Vector myScores = new Vector();
        Then write a while loop that reads every score on the file and adds it to the vector using

        Code:
         myScores.add(score);

        Comment

        • syncjc822
          New Member
          • Nov 2006
          • 3

          #5
          Thanks for replying, I'll try that

          Comment

          Working...