/**Already declared in main method: *TreeMap<String,TreeMap<String,Integer>> cla
ID: 3624389 • Letter: #
Question
/**Already declared in main method:*TreeMap<String,TreeMap<String,Integer>> classList =
*/ new TreeMap<String,TreeMap<String,Integer>>();
/**
* Add a student to the class list.
* If there already is a student with the same name, just print an error
* message.
*
* @param list The class list.
* @param name The student's name.
*/
public static void
addStudent(TreeMap<String,TreeMap<String,Integer>> list,
String name) {
classlist .put(1, "Ari Stotle");
if(classlist.1 != classlist.1) {
System.out.println(classlist.get(1));
}
else{
System.out.println("error");
}
} // end of addStudent method
Explanation / Answer
If you declared classlist in main(), it does not exist in addStudent(). You'll need to declare classlistoutside of main().
Also, since you insert before checking whether the sudent is in the map, you'll never catch an error.
/**
* Add a student to the class list.
* If there already is a student with the same name, just print an error
* message.
*
* @param list The class list.
* @param name The student's name.
*/
public static void addStudent(TreeMap<String,TreeMap<String,Integer>> list, String name) {
// check if student is in map
if(classlist.containsKey(name))
{
System.out.println("Error");
}
else
{
classlist.put(name, list);
}
} // end of addStudent method
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.