Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have a list of student ID\'s followed by the course number (separated by a s

ID: 3818962 • Letter: Y

Question

You have a list of student ID's followed by the course number (separated by a space) that the student is enrolled in. The listing is in no particular order. For example, if student 1 is in CS100 and CS200 while student 2 is in CS105 and MATH210 then the list might look like this: 1 CS100 2 MATH210 2 CS105 1 CS200 Write a program that reads data in this format from the console. If the ID is -1 then stop inputting data. Use the HashMap class to map from an Integer (the student ID) to an AirayList of type String that holds each class that the student is enrolled in. The declaration should look like this: HashMap students = new HashMap(); After all data is input, iterate through the map and output the student ID and all classes stored in the vector for that student. The result should be a list of classes organized by student ID.

Explanation / Answer

Java Program:-

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class HashMapCoursers {
  
   public static void main(String args[]) throws IOException{
      
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String Str[] = new String[2];
       HashMap<Integer, ArrayList<String>> list = new HashMap<Integer, ArrayList<String>>();
      
       while(true){
          
           Str = br.readLine().split(" ");                //Read Line by Line and Split the Line into Key and Value
          
           if(Str.length != 2)break;
          
           Integer courseID = Integer.parseInt(Str[0]);
          
            String Course = Str[1];                                      //After splitting   first String is CourseID and second is Course.
          
            ArrayList<String> myList = list.get(courseID);
          
            if(myList == null){                                        //If the List is empty initialize the list and add into the list.
               myList = new ArrayList<String>();
               myList.add(Course);
               list.put(courseID, myList);    
            }else{
               myList.add(Course);
               list.put(courseID, myList);                           //Else directly add to the List
            }
       }
       for (Map.Entry<Integer, ArrayList<String>> entry : list.entrySet())          //Iterate over the Map
       {
            System.out.print("Classes Organized by Student "+ entry.getKey()+" are :- ");
            ArrayList<String> Classes = new ArrayList<String>();
            Classes = entry.getValue();
            if(Classes == null){
               System.out.println("No Classes for this Student");
            }else{
               for(String Class : Classes){                  //If the Student's value List is not empty print all the Courses in list.
                   System.out.print(Class+"   ");
               }
               System.out.println("");
            }
       }
   }

}


Input:-

1 CS100
2 Math210
2 CS105
1 CS200

Output:-

Classes Organized by Student 1 are :- CS100   CS200
Classes Organized by Student 2 are :- Math210   CS105  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote