This is supposed to be Savitch Edition 7, Chapter 12, Practice Programming, #15,
ID: 3759346 • Letter: T
Question
This is supposed to be Savitch Edition 7, Chapter 12, Practice Programming, #15, but it's not up yet. I was wondering if I could get a little help
Program Description:
You have a list of IDs (strings), followed by the course number that a 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 n CS105 and MATH210 then the list could look something like:
1 CS100
2 MATH210
3 CS105
4 CS200
Write a program that reads the data in from the console. If the ID is -1, then stop.
Use the HashMap class to map from a String to an ArrayList of type String that holds each class that the student is enrolled in. The declaration will look like this:
HashMap<String, ArrayList<String>> students = new HashMap<String, ArrayList<String>>();
After all the data is input, iterate through the map and output the ID and all the classes stored in the ArrayList for that student. The results should be a list of classes organized by ID.
Explanation / Answer
import java.util.Scanner;
import java.util.*;
class Student
{
public static void main(String args[])
{
int a = 0;
float b;
String s;
ArrayList<String> al=new ArrayList<String>();
Map<String, ArrayList<String>> dictMap = new HashMap<String, ArrayList<String>>();
Scanner in = new Scanner(System.in);
do{
System.out.println("Enter a Student Id");
s = in.nextLine();
System.out.println("Enter a Subject ID");
al.add(in.nextLine());
dictMap.put(s, a1); // Error getting here I will solve later
/* for(String obj:al)
System.out.println(obj); */
System.out.println("Stop Enter -1 integer");
a = in.nextInt();
}while(a != -1);
for(Map.Entry m:dictMap.entrySet()){
System.out.println(m.getKey()+" "+m.getValue()); }
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.