Putting it together
Lists, arrays and strings often work together. The program below reads quiz results as name,score lines until an empty line, and then prints everyone who scored above the average. The average is known only after the last line, so the program has to remember every name and score until then: that's what the two lists are for.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> names = new ArrayList<>();
ArrayList<Integer> scores = new ArrayList<>();
while (true) {
String line = scanner.nextLine();
if (line.isEmpty()) {
break;
}
String[] parts = line.split(",");
names.add(parts[0]);
scores.add(Integer.valueOf(parts[1].trim()));
}
int sum = 0;
for (int score : scores) {
sum += score;
}
double average = (double) sum / scores.size();
System.out.println("Average: " + average);
System.out.println("Above the average:");
for (int i = 0; i < names.size(); i++) {
if (scores.get(i) > average) {
System.out.println(names.get(i) + " (" + scores.get(i) + ")");
}
}
}
}
Aino,72
Kai, 55
Lumi,90
Noor,61
(an empty line)Average: 69.5
Above the average:
Aino (72)
Lumi (90)- Each line is split into an array of two parts. The name goes into one list and the score into the other, so the same index points to the same person in both lists.
trim()lets the program acceptKai, 55as well asKai,55.- The last loop goes through the indexes, so it can look up the name and the score of the same person.
The end of part 3
This step ends the material of the MOOC's third part. Part 3 was about handling more data than a few variables can hold. It started with finding bugs, and then brought lists, which grow as values are added, and arrays, whose size is fixed when they're made. A loop goes through either of them one element at a time. This module added the tools for taking text apart: its characters, its pieces and split. Together they let a program read any amount of input and make sense of it. The MOOC closes the part with a summary page on its own site, for reading only.