Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Read File, calculate, then add to array.

$
0
0
My project has names and scores for 16 students in a class. A text file has their names and their lab/quiz scores. My question is trying to tie in the logic of coding to actual written code.

Example: We are asked to have parallel arrays storing student names, lab scores, quiz scores, project scores, and so on. Here is a snippet of what the text file looks like.

Line 1: Puckett, Karen
Line 2: 10 10 9.5 10 10 8 9.5 10 10 10 9 10 10 10 0
Line 3: 4 3 5 3 5 2 3 2 1.5 1 5 3.5
Line 4: 17.5 24 22 23.5 22 23
Line 5: 90 91
Line 6: 96

My underlying question is this: Line 2 consists of 15 lab scores. I want to set up an array to store only the average lab score for each of the 16 students. How can I calculate Line 2's average (sum/15) while I am reading from the file.

This was my attempt, which doesn't work, because I think my logic is somehow incorrect.
I don't think I can simply try to divide by 15 after the inputFile.nextDouble() command. Netbeans didn't flag it, but it also doesn't work. Any help would be appreciated.

*Note this is only partial code from a method.

        //Declare File Object
        File myFile = new File("scores.txt");
        if (!myFile.exists())
            {
                System.out.println("Unable to open scores.txt");
                System.exit(0);
            }
        
        //Declare Scanner Object
        Scanner inputFile = new Scanner(myFile);
        
        //Read From File
        for (int i=0; i<16; i++)
        {
            studentNames[i] = inputFile.nextLine();
            labGrade[i] = (inputFile.nextDouble() /15.0);
            quizAverage[i] = (inputFile.nextDouble() /12.0);
            projectAverage[i] = (inputFile.nextDouble() /6.0);
            examAverage[i] = (inputFile.nextDouble() /2.0);
            lastExam[i] = inputFile.nextDouble();
        }


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>