public static void main(String[] args) {
HashMap<String, Double> students = new HashMap<>();
Scanner in = new Scanner(System.in);
String newStudent;
System.out.println("Enter your students (or ENTER to finish):");
// Get student names and grades
do {
System.out.print("Student: ");
newStudent = in.nextLine();
if (!newStudent.equals("")) {
System.out.print("Grade: ");
Double newGrade = in.nextDouble();
students.put(newStudent, newGrade);
// Read in the newline before looping back
in.nextLine();
}
} while(!newStudent.equals(""));
// Print class roster
System.out.println("\nClass roster:");
Double sum = 0.0;
for (Map.Entry<String, Double> student : students.entrySet()) {
System.out.println(student.getKey() + " (" + student.getValue() + ")");
sum += student.getValue();
}
Double avg = sum / students.size();
System.out.println("Average grade: " + avg);
}
}`
As with lists, we can add a new item with a .add() method, but this time we must specify both key and value: students.add(newStudent, newGrade).
It doesn't look like the example uses .add() method. Looks like .put() is used instead.
`import java.util.HashMap; import java.util.Map; import java.util.Scanner;
public class GradebookHashMap {
}`
It doesn't look like the example uses .add() method. Looks like .put() is used instead.