Results 1 to 4 of 4
  1. #1
    delvinv is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Help with deleting/printing linkedlist<object>

    Hey everyone,

    I have a class called Student with attributes forename, surname [Strings] and StudentID[int].

    Now, i have created a class called Registry (essentially a registry of Student(s)).
    I have created a linkedlist called StudentList of type <Student>.

    Using format, toString() methods i can successfully print them, but i need help with deleting a particular student using their studentId.
    I wrote the following code

    public void deleteStudent(int studentId) {
    boolean found = false;
    Student aStudent = studentList.getFirst();
    it = studentList.listIterator();
    while (found == false)
    {
    if (aStudent.getStudentId()==studentId)
    {
    it.remove();
    System.out.println("Found and deleted!");
    found = true;
    }
    else if (it.hasNext())
    {
    aStudent = (Student) it.next();
    }
    else
    {
    System.out.println("Error! Search query not found!");
    found = true;
    }
    }

    }
    When i run it, it says successfully deleted, but when i run

    System.out.println(student2.format()); //this is the student who i supposedly deleted.

    I can still see the student in the linkedlist.

    Please correct me where i am going wrong,
    Last edited by delvinv; 05-01-2012 at 06:03 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Help with deleting/printing linkedlist<object>

    Please use [code] tags [/code] when posting code.
    Assuming that you actually go through the block with the remove() call then it will have been removed from that list.
    So, it'll be something in the code we cannot see that the problem lies.
    For example, where is student2 declared and assigned?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    delvinv is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help with deleting/printing linkedlist<object>

    I apologise for not putting it in code blocks. I have declared student2 in a seperate class called RegistryTester:
    Java Code:
    public class RegistryTester {
    
    public static void main (String[] args){
    
    Registry studentList = new Registry();    
    System.out.println("Testing student register");
    Student student1 = new Student("Ron", "Paul", 1001);
    studentList.addStudent(student1);
    Student student2 = new Student("Gary", "Shining", 1002);
    studentList.addStudent(student2);

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Help with deleting/printing linkedlist<object>

    OK, and is that the 'student2' variable you call format() on?
    Where do you call the method with the remove()?
    You need to give us a bigger picture to work with.
    For example:
    Java Code:
    Registry studentList = new Registry();
    Student student2 = new Student("Gary", "Shining", 1002);
    studentList.addStudent(student2); 
    deleteStudent(1002);
    System.out.println(student2.format());
    That would at least show me the flow and the logic for the relevant variables.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Printing object location instead of actual object.
    By Vampiricx3 in forum New To Java
    Replies: 6
    Last Post: 02-05-2012, 02:35 AM
  2. Replies: 18
    Last Post: 08-09-2011, 08:08 AM
  3. Printing out value of an object
    By jasonwucinski in forum New To Java
    Replies: 21
    Last Post: 02-15-2011, 09:10 AM
  4. Printing Object Information
    By dom12 in forum New To Java
    Replies: 10
    Last Post: 11-04-2010, 06:39 PM
  5. Deleting from an object
    By vitaminz in forum New To Java
    Replies: 7
    Last Post: 08-10-2008, 03:56 AM

Posting Permissions

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