Hi all, my assignment is to use TreeMap to sort Student objects. Students have a first name, a last name, and a unique integer ID. The sample program looks like so:
Sample Program
A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]A[/COLOR] Enter the student's first name: [COLOR="blue"]Joe[/COLOR] Enter the student's last name: [COLOR="Blue"]Smith[/COLOR] Enter the student's ID: [COLOR="blue"]1001[/COLOR] Enter the student's grade: [COLOR="blue"]C[/COLOR] A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]A[/COLOR] Enter the student's name: [COLOR="blue"]Sarah[/COLOR] Enter the student's last name: [COLOR="blue"]Smith[/COLOR] Enter the student's ID: [COLOR="blue"]1002[/COLOR] Enter the student's grade: [COLOR="blue"]B+[/COLOR] A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]A[/COLOR] Enter the student's first name: [COLOR="blue"]Joe[/COLOR] Enter the student's last name: [COLOR="blue"]Smith[/COLOR] Enter the student's ID: [COLOR="blue"]2003[/COLOR] Enter the student's grade: [COLOR="blue"]B+[/COLOR] A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]A[/COLOR] Enter the student's first name: [COLOR="blue"]Harry[/COLOR] Enter the student's last name: [COLOR="blue"]Pham[/COLOR] Enter the student's ID: [COLOR="blue"]1996[/COLOR] Enter the student's grade: [COLOR="blue"]F[/COLOR] A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]M[/COLOR] Enter the student's ID: [COLOR="blue"]1002[/COLOR] Enter the student's grade: [COLOR="blue"]A[/COLOR] A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]R[/COLOR] Enter the student's ID: [COLOR="blue"]1996[/COLOR] A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]P[/COLOR] Joe Smith 1001: C Joe Smith 2003: B+ Sarah Smith 1002: A A)dd R)emove M)odify P)rint Q)uit [COLOR="blue"]Q[/COLOR]
When I add a student, it is supposed to sort it by last name first, then by first name, and then by student ID number. I was able to do this just fine, however I ran into problems when trying to remove, modify, or print out my list. Here are my Student and GradeBook classes:
Student
public class Student { public Student( String aFirstName, String aLastName, int aStudentId ) { firstName = aFirstName; lastName = aLastName; studentId = aStudentId; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getStudentId() { return studentId; } public String toString() { return firstName + " " + lastName + " " + studentId; } private String firstName; private String lastName; private int studentId; }
GradeBook
import java.util.Scanner; import java.util.TreeMap; import java.util.Map; import java.util.Set; import java.util.Comparator; public class GradeBook5 { public static void main(String[] args) { Scanner in = new Scanner(System.in); class StudentComparator implements Comparator<Student> { public int compare( Student a, Student b ) { if( a.getLastName().compareTo(b.getLastName() ) < 0 ) return -1; if( a.getLastName().compareTo(b.getLastName() ) == 0 ) { if( a.getFirstName().compareTo(b.getFirstName() ) < 0 ) return -1; if( a.getFirstName().compareTo(b.getFirstName() ) == 0 ) { if( a.getStudentId() < b.getStudentId() ) return -1; return 1; } return 1; } return 1; } } Comparator<Student> comp = new StudentComparator(); Map<Student, String> students = new TreeMap<Student, String>(comp); Map<Integer, Student> id = new TreeMap<Integer, Student>(); System.out.println( "A)dd R)emove M)odify P)rint Q)uit" ); String answer = in.nextLine(); while( !answer.equalsIgnoreCase("Q") ) { if( answer.equalsIgnoreCase("A") ) { System.out.println( "Enter the student's first name:" ); String firstName = in.nextLine(); System.out.println( "Enter the student's last name:" ); String lastName = in.nextLine(); System.out.println( "Enter the student's ID:" ); int studentId = in.nextInt(); String trash = in.nextLine(); System.out.println( "Enter the student's grade:" ); String grade = in.nextLine(); Student s = new Student( firstName, lastName, studentId ); students.put( s, grade ); id.put( studentId, s ); } else if( answer.equalsIgnoreCase("R") ) { System.out.println( "Enter the student's ID:" ); int studentId = in.nextInt(); String trash = in.nextLine(); Student s = id.get( studentId ); System.out.println( students.remove( s ) ); id.remove( studentId ); } else if( answer.equalsIgnoreCase("M") ) { System.out.println( "Enter the student's ID:" ); int studentId = in.nextInt(); String trash = in.nextLine(); Student s = id.get( studentId ); System.out.println( "Enter the student's grade:" ); String grade = in.nextLine(); students.put( s, grade ); id.put( studentId, s ); } else if( answer.equalsIgnoreCase("P") ) { Set<Student> keySet = students.keySet(); System.out.println( students.keySet() ); for( Student key : keySet ) { String value = students.get( key ); System.out.println( key.toString() + ": " + value ); } } System.out.println( "A)dd R)emove M)odify P)rint Q)uit" ); answer = in.nextLine(); } } }
When printing out the list, it does not seem to want to print out the grade (value of the students TreeMap). Also, I cannot seem to remove a Student from either TreeMap nor can I modify them. What am I doing wrong?