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

This is the second problem I need help with, please. Problem 2 You have a list o

ID: 3763601 • Letter: T

Question

This is the second problem I need help with, please.

Problem 2 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 thi:s 1 CS100 2 MATH210 2 CS105 1 CS200 Write a method that reads data in this format from enrollments,txt. An ID of -1 is the flag to stop reading data. Echo the input to the console as you read it (use a tabular layout). Use the HashMap class to map from an Integer (the student ID) to an ArrayList of type String to hold each class in which that the student is enrolled. The declaration should be: HashMap students = HashMap

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;

public class Data {
   public static void main(String[] args)
   {
       BufferedReader br=null;
       String s="";
try{
br   =new BufferedReader(new FileReader("enrollments.txt"));
s= br.readLine();
}
catch(Exception e)
{
  
   System.out.println(e);
}

   int id=Integer.parseInt(s.substring(0, s.indexOf(" ")));
String course=(s.substring(s.indexOf(" ")+1));
   System.out.println(id+" "+course);
  

HashMap<Integer, ArrayList<String>> students= new HashMap<Integer, ArrayList<String>>();
while(id!=-1)
{
   if(students.containsKey(id))
   {
       students.get(id).add(course);
      
   }
  
   else{
   ArrayList<String> list=new ArrayList<String>();
list.add(course);
students.put(id, list);
   }
try{  
s=br.readLine();
}
catch(Exception e)
{
  
   System.out.println(e);
}
if(s.indexOf(" ")!=-1){
   id=Integer.parseInt(s.substring(0, s.indexOf(" ")));
course=(s.substring(s.indexOf(" ")+1));
    System.out.println(id+" "+course);

}
else{
   id=-1;
System.out.println(id);
}
  
}
  
Set lset = students.entrySet();
Iterator<Set> lItr = lset.iterator();
System.out.println("All Data");
System.out.println("----------------");


while(lItr.hasNext())
{
Map.Entry<Integer,ArrayList<String>> KeyValuePair = (Map.Entry)lItr.next();

int key1=KeyValuePair.getKey() ;
   System.out.print("Student Id: ");

if(String.valueOf(key1).length()<5)
{
    for(int i=1;i<=5-String.valueOf(key1).length();i++)
        System.out.print("0");

   
}
      System.out.println(key1);
      System.out.println("Classes");
ArrayList<String> output=students.get(key1);
for(int i=0;i<output.size();i++)
{
      System.out.println("   "+output.get(i));

  
}

}

  
   }
  
  
}

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