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= 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
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started