Question: I have this hw problem: Write a program that keeps a TreeMap in which both keys and values are stringsthe names of students and their

I have this hw problem:

Write a program that keeps a TreeMap in which both keys and values are stringsthe names of students and their course grades. Prompt the user of the program to add or remove students, to modify grades, or to print all grades. Remember that if two students have the same name, then the grade value should be used as the factor to sort.

The printout should be sorted by name and formatted like this: Cara: 90 Cara: 87 John: 80 Meredith: 90

and I have this code, but I'm getting errors:

import java.util.*;

public class StudentMap implements Comparable { SortedMap> studentGrades; Scanner scnr = new Scanner(System.in); StudentMap(){

studentGrades= new TreeMap< >(); int menuChoice; do { System.out.println("Please choose an option: "); System.out.println("1: Add Student"); System.out.println("2: Remove Student"); System.out.println("3: Modify Grade"); System.out.println("4: Print All Grades"); System.out.println("Press 0 to Exit"); System.out.print("Your option: "); menuChoice = Integer.parseInt(scnr.nextLine());

switch(menuChoice) { case 1: addStudent(); break; case 2: removeStudent(); break; case 3: modifyGrade(studentGrades); break; case 4: printAllGrades(); break; case 0: return; default: System.out.println("Invalid Option"); break; } } while (menuChoice!= 0); } public static void modifyGrade(SortedMap> studentGradesMod) { String studentName; String studentGrade; Scanner scnr = new Scanner(System.in);

System.out.println("Please enter the students name: "); studentName = scnr.nextLine();

System.out.println("Please enter the students modified grade: "); studentGrade = scnr.nextLine();

if(studentGradesMod.containsKey(studentName)) { studentGradesMod.get(studentName).add(studentGrade); System.out.println(studentName + "s new grade is " + studentGrade); } else { System.out.println("Student does not exist, please re-enter students name and modified grade."); } }

public void addStudent() { Scanner scnr = new Scanner(System.in); System.out.println("Enter name of student you would like to add: "); String studentName = scnr.next(); System.out.println("Enter students grade: "); String studentGrade = scnr.next();

if (studentGrades.get(studentName) == null) { studentGrades.put(studentName, new ArrayList<>()); } else { studentGrades.comparator(); } studentGrades.get(studentName).add(studentGrade); System.out.println(studentName + " has been added."); }

public void removeStudent() { Scanner scnr = new Scanner(System.in); System.out.println("Enter name of student you would like to remove: "); String studentName = scnr.next();

if(studentGrades.containsKey(studentName)) { studentGrades.remove(studentName); System.out.println(studentName + " has been removed."); } else { System.out.println("Student does not exist, please re-enter students name."); } }

public void printAllGrades() { Iterator iterator = studentGrades.keySet().iterator(); while (iterator.hasNext()) { String studentName = iterator.next().toString(); List grades = studentGrades.get(studentName); for(String studentGrade: grades) { System.out.println(studentName + " : " + studentGrade); } } }

public static void main (String[] args) { new StudentMap(); }

public int compareTo(Object arg0) { return 0; } }

can someone please help me fix my code and tell me what I was doing wrong?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!