LaunchCodeEducation / skills-back-end-java

Unit 3 (Java Track) of the LC101 Curriculum, following fundamentals in Python
http://education.launchcode.org/skills-back-end-java/
Other
16 stars 31 forks source link

.put() in example code vs .get() in expaination #5

Closed janglehorse closed 7 years ago

janglehorse commented 7 years ago

`import java.util.HashMap; import java.util.Map; import java.util.Scanner;

public class GradebookHashMap {

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.

chrisbay commented 7 years ago

Fixed in 49478e3599109a9f46971720cd84d36d4cd57383

Thanks!