Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package lab7;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class QuizAverager
{
public static void main(String[] args) throws FileNotFoundException
{
int pointsPossible = 50;
// open the file
File file = new File("scores.txt");
Scanner scanner = new Scanner(file);
// while there are more lines...
while (scanner.hasNextLine())
{
// get the next line
String line = scanner.nextLine();
// process the line
findAverage(line, pointsPossible);
}
// close the file
scanner.close();
}
private static void findAverage(String line, int pointsPossible)
{
// construct a temporary scanner, just to read data from this line
Scanner temp = new Scanner(line);
// get the first and last name
String first = temp.next();
String last = temp.next();
// add up all the scores
double total = 0.0;
while (temp.hasNextInt())
{
int value = temp.nextInt();
total += value;
}
// print the average
double average = total / pointsPossible;
System.out.println(first + " " + last + " " + average);
}
}