Results 1 to 4 of 4
- 05-01-2012, 06:01 PM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
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.
- 05-01-2012, 06:24 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
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.
- 05-01-2012, 07:22 PM #3
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
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);
- 05-02-2012, 09:43 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
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:
That would at least show me the flow and the logic for the relevant variables.Java Code:Registry studentList = new Registry(); Student student2 = new Student("Gary", "Shining", 1002); studentList.addStudent(student2); deleteStudent(1002); System.out.println(student2.format());Please do not ask for code as refusal often offends.
Similar Threads
-
Printing object location instead of actual object.
By Vampiricx3 in forum New To JavaReplies: 6Last Post: 02-05-2012, 02:35 AM -
Having trouble printing the LinkedList correctly..
By teekei in forum New To JavaReplies: 18Last Post: 08-09-2011, 08:08 AM -
Printing out value of an object
By jasonwucinski in forum New To JavaReplies: 21Last Post: 02-15-2011, 09:10 AM -
Printing Object Information
By dom12 in forum New To JavaReplies: 10Last Post: 11-04-2010, 06:39 PM -
Deleting from an object
By vitaminz in forum New To JavaReplies: 7Last Post: 08-10-2008, 03:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks