Java Retrieve or Remove a value from TreeMap -
i have problem retrieving student's info list, or deleting it... heelp... code below:
import java.util.*; public class directory { private treemap<string, student> studentlist; private int numberofentries; public directory() { studentlist = new treemap<string, student>(); numberofentries = 0; } public void addstudent(student newstudent) { studentlist.put(newstudent.studentinfo(), newstudent); //numberofentries++; } public void studentinfo(string studentinfo) { object obj = studentlist.get(studentinfo); system.out.println(obj); } public void removestudent(string studentinfo) { object obj = studentlist.remove(studentinfo); system.out.println(obj + "removed"); } public void printstudentlist() { system.out.println("list of students: " + studentlist.keyset()); } }
======= student class ======== (persons contains first, last & email)
public class student extends persons { private string sclass; public student(string lname, string fname, string email, string sclass) { super(lname, fname, email); this.sclass = sclass; } public string studentinfo() { return " full name " + lastname + " " + firstname + "\n" + "e-mail: " + email + "\n" + "class attending: " + sclass; } public string getname() { return lastname; } }
i try , debug you, defeat purpose. (this your homework ... , purpose you learn how yourself.)
my advice follows:
first fix style errors:
the names of variables should always start lower-case letter ... unless
static
final
constants.the names of methods should always start lower-case letter
method , variable names should meaningful , consistent. instance:
public void removestudent(string studentinfo)
here
studentinfo
needs student name, not "student info" string createdstrudentinfo
method ...another example:
lname
,fname
not meaningful. (i can guess mean, not enough.)
create tester program created instances of classes , performs sequence of tests on them. start simple things, move on more complicated ones.
(in real world, we'd set more formal set of "unit tests" ... not ready yet.
in fact, if choose more meaningful names, , carefully @ how names used, error should "leap out , hit on nose".
but maximum benefit if go through process yourself.
Comments
Post a Comment