Lesson 6.10 · Answer it
Top score as text
4 marksYou mark it yourself against the marking points
The file scores.csv holds three lines: Ana,85, then Bo,100, then Cy,9. Scores are whole numbers from 1 to 100. This program (in Java, the lines are inside a main that throws IOException, with the imports it needs) is meant to print the name of the student with the highest score. It runs with no error.
best_name = ""
best_score = ""
with open("scores.csv") as f:
for raw in f:
fields = raw.rstrip("\n").split(",")
if fields[1] > best_score:
best_name = fields[0]
best_score = fields[1]
print(best_name)
String bestName = "";
String bestScore = "";
try (BufferedReader f = new BufferedReader(new FileReader("scores.csv"))) {
String line = f.readLine();
while (line != null) {
String[] fields = line.split(",");
if (fields[1].compareTo(bestScore) > 0) {
bestName = fields[0];
bestScore = fields[1];
}
line = f.readLine();
}
}
System.out.println(bestName);
(a) State the name the program prints. [1]
(b) Explain why it does not print Bo. [2]
(c) State the change that makes the program correct. [1]
[4]