0

For example, i have inputted 3 string key values

Enter student number 1: 2018-0004

Enter student number 2: 2018-0017

Enter student number 3: 2018-0134

Student List:

2018-0004

2018-0017

2018-0134

In this case, I want to remove the third key value. Like this:

Student List:

2018-0004

2018-0017

Here's my code:

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

public class StudentList {
        
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<String, String> students = new HashMap<>();
        String student_num;
        int value = 3;
        
        for(int i = 1; i <= 3; i++){
            System.out.print("Enter student number " + i + ": ");
            student_num = sc.nextLine();
            students.put(student_num);
        }
        
        System.out.println("Student List:");
        for(Map.Entry e: students.entrySet()){
            System.out.println(e.getKey());
        }
        
        for(Entry<String, Integer> entry: students.entrySet()){
            if(entry.getValue() == value)
            System.out.println(entry.remove());
        }
        
        System.out.println("Student List:");
        for(Map.Entry e: students.entrySet()){
            System.out.println(e.getKey());
        }
        
        
    }
}

And this is the code for remove method:

for(Entry<String, String> entry: students.entrySet()){
            if(entry.getValue() == value)
            System.out.println(entry.remove());
        }

0 Answers0