Page 1 of 2 12 LastLast
Results 1 to 20 of 35
Like Tree1Likes

Thread: Retrieve a Student or Delete it from TreeMap issue.

  1. #1
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Retrieve a Student or Delete it from TreeMap issue.

    Hello,

    I'm really new to this and cannot figure out what I got wrong. I need to be able to view a single student's info, as well as delete a student by name, but when I try it I get "null"... What am I doing qrong?

    Main class code:

    Java Code:
    import java.util.*;
    public class Directory
    {
        private TreeMap<String, Student> studentList;
    
        public Directory()
        {
            studentList = new TreeMap<String, Student>();
        }
    
        public void addStudent(Student newStudent)
        {
            studentList.put(newStudent.StudentInfo(), newStudent);
        }
    
        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 and email)

    Java Code:
    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;
        }
    }
    Any help is greatly appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    You addStudent method needs to be:

    studentList.put(newStudent.getName(), newStudent); // you want to store by name.

    When you retrieve a value, use their Lastname as a key. Same for deleting.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Hello Jim,

    Thank you very much for that answer. It sure helped me a lot.

    Now I'm stuck on the way it prints: It shows the Student's last name along with a sequence of numbers. How can I fix that?

    Thanks in advance!


    Java Code:
        public void addStudent(Student newStudent)
        {
            studentList.put(newStudent.getName(), newStudent);
        }
        
        public void studentInfo(Student studentName)
        {
            Object obj = studentList.get(studentName.getName());
            System.out.println(obj);
        }
        
        public void removeStudent(Student studentName)
        {
            Object obj = studentList.remove(studentName.getName());
            System.out.println(obj + "Removed");
        }
    
        
        public void printStudentList()
        {   
            Set st = studentList.keySet();
            System.out.println("List of Students: ");
            
            Iterator itr = st.iterator();
            while(itr.hasNext())
                System.out.println(itr.next());
                
        }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Can you show what the output is?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Set is a generic type so you should declare it as a Set<String>. The iterator is also of type String. Use the itr.next() value as the key to your treemap. Then use the returned object to print out studentInfo().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Curious where the "sequence of numbers" is coming from.
    If that's the keyset (which it is) and the keys are Strings (which they appear to be) then there's noweher obvious, apart from as part of the String, that that "sequence of numbers" could come from.

    In any case, when iterating over a Map with the intention of using both keys and values then it is generally best to use the entrySet() rather than iterating over the keys then accessing the value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    This is what I get when I enter "Gates Bill Email, whatever, class, whatever too" (the other is Smith Will)... Just random names, no comment lol

    List of Students:
    Gates=Student@655d6184
    Smith=Student@55ad5519

    This is the code snip:
    Java Code:
        public void printStudentList()
        {   
            Set String = studentList.entrySet();
            System.out.println("List of Students: ");
            
            Iterator itr = String.iterator();
            while(itr.hasNext())
                System.out.println(itr.next());
                
        }

  8. #8
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    I get same string when retrieving studentInfo btw

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    That's strange because StudentInfo() returns a string which should contain the information you want for each Student object. So this is what you are doing (before you changed your code to EntrySet)?

    Java Code:
    public void printStudentList()
    {   
        Set<String> st = studentList.keySet();
        System.out.println("List of Students: ");
         
        Iterator<String> itr = st.iterator();
        while(itr.hasNext()) {
             Student student = studentList.get(itr.next());
             System.out.println(student.StudentInfo()); 
        }
             
    }

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Thank you Jim! That makes better sense, n works.

    I do however still get the numbers with studentInfo and removeStudent.

    Java Code:
        public void studentInfo(Student studentName)
        {
            Object obj = studentList.get(studentName.getName());
            System.out.println(obj.studentInfo());
        }
       
        public void removeStudent(Student studentName)
        {
            Object obj = studentList.remove(studentName.getName());
            System.out.println(obj + "Removed");
        }
    First one: studentInfo I get compiling error on the .studentInfo (in the println)

    Second one: removeStudent works, but it displays that sequence of numbers rather than the Student's info...

    I'm lost after reading the code over n over n over n over

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    In the first case, you need to use StudentInfo() (note uppercase S). That's why you got a compiler error.

    In the second case when you remove a value, it returns the object of the type removed, in this case Student. So the best practice here is to
    first check to see if null was returned which indicates that no entry was found. If an entry was found you can use the returned object to print something like the students name, which was the key to the entry in the first place. If null was returned, you can print some informative message too.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Dear Jim,

    I get the compile error with and without cap "S", and I fixed it to be studentInfo when you guys brought it up earlier too.

    Second one, that is where I'm stuck, I want it to print student's name, which is why (Or so I thought) I made the Object obj to get the student's name on the line above, yet it returns that @bunchanumbers thing, which are assigned to that student, but its just not the info it should display... Any help on those 2 lat bits would be greatly appreciated.

    Thanks!

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Please show me the error. If you are using the correct object type (i.e. Student) then there should be no compiler error. Did you change the return type from Object to Student? Or perhaps I made a mistake.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Okay, for first one:
    Java Code:
        public void studentInfo(Student studentName)
        {
            Object obj = studentList.get(studentName.getName());
            System.out.println(obj.studentInfo());
        }
    Error: Cannot find symbol - method studentInfo()

    --------------------

    Second one:

    Java Code:
        public void removeStudent(Student studentName)
        {
            Object obj = studentList.remove(studentName.getName());
            System.out.println(obj + "Removed");
        }
    Terminal says: Student@6554598eRemoved

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Change Object obj to Student obj.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Nopes, same result for both cases

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Okay, please post your current code and error messages.

    I just noticed you are passing a Student object. This will work but it makes no sense because you already have the information (in the form of a Student object that you just passed). What you want to do is pass the name of the student to lookup.

    Java Code:
    public void studentInfo(String studentName)
    {
        Student obj = studentList.get(studentName);
        if (obj != null) {
               System.out.println(obj.studentInfo());
        } else {
                System.out.println(studentName + " not found.");
        }
    }
    Regards,
    Jim
    Last edited by jim829; 08-02-2013 at 01:53 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Student does not have a toString() method, which is why you are getting those numbers as it defaults to the toString on Object which simply prints the hashcode.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Good morning to you both Jim and Toll, and thank you for you patience.

    I got my code to work now for the issue mentioned above (thanks to your advise), but have a couple more questions:

    I'm supposed to be able to update entries (which I assume means change entry name n such?) so, I create say 5 students n add them to the directory; if I want to change the Student's info, can I do it through the directory, or only in each student's instance?

    Thanks again!

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Assuming I understand the question you always update a student either by replacing the current instance or modifying the current instance for a particular student. Whether that instance is returned via Directory methods or any other class method which has access to the underlying map does not matter. Note: If you retrieve a student instance and not a copy, then you can simply make changes to the instance and it will be reflected in the map. If you return a copy, then you are simply changing the copy and need to replace the current instance. If you change their name for some reason, then you should probably delete the old record and add the new one since names are used as keys.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 02-11-2013, 02:29 PM
  2. Treemap's
    By Russo in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 02:13 AM
  3. Replies: 2
    Last Post: 04-20-2009, 09:00 AM
  4. Delete Student from Array Help
    By kazit in forum New To Java
    Replies: 4
    Last Post: 02-25-2009, 03:42 AM
  5. Store retrieve and delete
    By on7june in forum New To Java
    Replies: 1
    Last Post: 03-08-2008, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •