-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathHashMapPractice.java
More file actions
35 lines (27 loc) · 1.02 KB
/
HashMapPractice.java
File metadata and controls
35 lines (27 loc) · 1.02 KB
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
package exercises.Lesson2;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HashMapPractice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
HashMap<Integer, String> studentRoster = new HashMap<>();
String newStudent = "";
do {
System.out.print("Student: ");
newStudent = input.nextLine();
if (!newStudent.equals("")) {
System.out.print("ID: ");
Integer newID = input.nextInt();
studentRoster.put(newID, newStudent);
input.nextLine();
}
} while(!newStudent.equals(""));
input.close();
System.out.println("\nClass roster:");
for (Map.Entry<Integer, String> student : studentRoster.entrySet()) {
System.out.println(student.getValue() + "'s ID: " + student.getKey());
}
System.out.println("Number of students in roster: " + studentRoster.size());
}
}