Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

java program that reads data from the console and iterate through the map and output the student ID number and all courses stored in the vector for that student

You have a list of student ID numbers followed by the course number (separated
by a space) that each 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 java program that reads data in this format from the console. If the ID number
is −1, then stop inputting data. Use the HashMap class to map from an Integer
(the student ID number) to an ArrayList of type String that holds each course
that the student is enrolled in. The declaration should look like this:
HashMap<Integer, ArrayList<String>> students =
new HashMap<Integer, ArrayList<String>>();
After all the data is input, iterate through the map and output the student ID
number and all courses stored in the vector for that student. The result should be
a list of courses organized by student ID number.

Answer:


import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;

public class Question10
{
	public static void main(String[] args)
	{
		HashMap<Integer, ArrayList<String>> students =
			new HashMap<Integer, ArrayList<String>>();
		String line;
		int studentID;
		String course;
		Scanner keyboard = new Scanner(System.in);

		System.out.println("Enter student ID followed by course number, -1 to exit.");
		do
		{
			line = keyboard.nextLine();
			line = line.trim(); // remove any leading or trailing whitespace
			if (!line.equals("-1"))
			{
				// Extract the ID and the Class, separated by a space,
				// by using the split method, which creates an array of strings
				// delimited by the delimiter (specified as a space in this case)
				String[] parsedString = line.split(" ");
				studentID = Integer.parseInt(parsedString[0]); // the ID
				course = parsedString[1];
				// Add to arraylist if student ID has been seen already
				if (students.containsKey(studentID))
				{
					students.get(studentID).add(course);
				}
				else
				{
					// Add arraylist for the first time to the map
					ArrayList<String> courses = new ArrayList<String>();
					courses.add(course);
					students.put(studentID, courses);
				}
			}
		} while (!line.equals("-1"));

		// Now iterate through the map and output the student ID and all courses
		// associated with the student
		System.out.println();
		for (Integer key : students.keySet())
		{
			System.out.println("For student: " + key);
			for (String cls : students.get(key))
			{
				System.out.print(cls + " ");
			}
			System.out.println();
			System.out.println();
		}
	}
}


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!