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

reading data into vectors

P: 3
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..




Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5.  
  6. public class ProcessScoresV1{
  7.  
  8.     public static void main(String[] args)throws FileNotFoundException {
  9.  
  10.         Scanner infile = new Scanner(new FileReader("scores.txt"));
  11.  
  12.         int count = 0;
  13.                 while (infile.hasNext()) {
  14.                     String word = infile.next();
  15.                         count++;
  16.                 }
  17.  
  18.         final long SENTINEL = -999;
  19.         double averageTotal = 0.0;
  20.         double averageLow = 0.0;
  21.         double averageHigh =0.0;
  22.  
  23.  
  24.         while (infile.hasNext()) {
  25.  
  26.                     double scoreTotal = 0;
  27.                     long scoreCount = 0;
  28.                     double score;
  29.  
  30.         while (infile.hasNextDouble()) {
  31.                 score = infile.nextDouble();
  32.                 scoreTotal += score;
  33.                 scoreCount++;
  34.  
  35.  
  36.             double average = scoreTotal/scoreCount;           // computes average of all scores
  37.             averageTotal += average;
  38.  
  39.                             //high score variables
  40.                 double scorehigh;
  41.                 long scoreCounthigh = 0;
  42.                 double scoreTotalhigh = 0;
  43.  
  44.                 // low score variables
  45.                 double scorelow;
  46.                 long scoreCountlw = 0;
  47.                 double scoreTotallw = 0;
  48.  
  49.  
  50.             if (score < 70) {                    //condition statement to compute low scores
  51.                 scorelow = infile.nextDouble();
  52.                 scoreTotallw += scorelow;
  53.                 scoreCountlw++;
  54.  
  55.             double averageL = scoreTotallw/scoreCountlw;  // computes averages of low scores
  56.             averageLow += averageL;
  57.  
  58.  
  59.             if (score > 70) {                  //condition statement to compute high scores
  60.               scorehigh = infile.nextDouble();
  61.               scoreTotalhigh+= scorehigh;
  62.               scoreCounthigh++;
  63.  
  64.  
  65.             double averageH = scoreTotalhigh/scoreCounthigh; // computes averages of high scores
  66.             averageH += averageHigh;
  67.  
  68.             JOptionPane.showMessageDialog(null, "Score Total" +  scoreTotal + "Total Average Score" + average);
  69.             JOptionPane.showMessageDialog(null, "High scores" +  scoreTotalhigh + "Average High Scores" + averageH);
  70.             JOptionPane.showMessageDialog(null, "Low scores" +  scoreTotallw + "Average Low Scores" + averageL);
  71.             JOptionPane.showMessageDialog(null, "Number of Total Scores" + count);
  72.  
  73.  
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80.  
Nov 29 '06 #1
Share this Question
Share on Google+
4 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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?
Nov 29 '06 #2

P: 3
im not sure how to go about adding vectors in my program
Nov 29 '06 #3

10K+
P: 13,264
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
Expand|Select|Wrap|Line Numbers
  1.  Vector myScores = new Vector();
Then write a while loop that reads every score on the file and adds it to the vector using

Expand|Select|Wrap|Line Numbers
  1.  myScores.add(score); 
Nov 30 '06 #4

P: 3
Thanks for replying, I'll try that
Nov 30 '06 #5

Post your reply

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