Question

I was given the following task and have a small idea of how to do it but I cannot seem to code it properly. Please give a simple solution in Java.

Create the following text file called studentScores.in and store it in a place so your program can access it.

File contents:

Agnes 56 82 95 100 68 52

Bufford 87 92 97 100 96 85 93 77 98 86 

Julie 99 100 100 89 96 100 92 99 68 

Alice 40 36 85 16 0 22 72

Bobby 100 98 92 86 88

Each line of the file consists of a student’s name followed by an unpredictable number of test scores. The number of students is also unpredictable. The desired output is shown below where the numbers displayed represent the average test score rounded to the nearest whole number. Create a class called StudentAverages that will input the studentScores.in text file and produce the output shown below:

Agnes, average = 76 

Bufford, average = 91 

Julie, average = 94 

Alice, average = 39 

Bobby, average = 93
Was it helpful?

Solution

Here's a simple algorithm to get you in the right direction.

  • read file line by line
  • using StringTokenizer break up each line on " "
  • the first token will be the name
  • the rest of the tokens will be the scores. Parse these as integers and compute the average
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top