Forums Register Login

toString() using queues with nodes

+Pie Number of slices to send: Send
Hello all,
I'm trying to print the contents of a queue using nodes but I get duplicates for some reason and I don't understand why. Here is my code:




The unbounded interface just overrides a queue interface where the enqueue, dequeue, size, and toString methods are. The console prints the last item added to the queue. For example, if I add Mary with ID 10, Jon with ID 20, and Adrian with ID 30, the program will print:
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30

Please help me by pointing where the error/bug is, tell me in a conceptual way so I see what's wrong and to code it.  
+Pie Number of slices to send: Send
Where is the loop in your demo class?  Can you make sure you posted the same classes that you tested?
+Pie Number of slices to send: Send
Based on what you described, it looks like you're only using the one instance of Student that you created on line 5 in main(). To fix the problem, you need to do a new Student() for each new student you create and add to your queue. Otherwise, you're going to have all your Nodes referencing the same Student object, which is why you see the same output for all nodes.
+Pie Number of slices to send: Send
 

Adrian Meneses wrote:Hello all,
I'm trying to print the contents of a queue using nodes but I get duplicates for some reason and I don't understand why. Here is my code:




The unbounded interface just overrides a queue interface where the enqueue, dequeue, size, and toString methods are. The console prints the last item added to the queue. For example, if I add Mary with ID 10, Jon with ID 20, and Adrian with ID 30, the program will print:
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30

Please help me by pointing where the error/bug is, tell me in a conceptual way so I see what's wrong and to code it.  


I added the do-while loop in the demo class that I forgot to post. The program, as I see it, adds a new student to the queue but when it comes to print all the students in the queue it only prints the last student added multiple times.
1
+Pie Number of slices to send: Send
In your main() method:

This line is executed outside of your loop and that means it gets executed EXACTLY one time.  Now, since you only have one instance of a Student object, here's what basically happens:

You create Node1 and make it refer to the Student object as its data. However, you never instantiate another Student object. So, next time you use the newStudent reference, you're still referring to the Student object you created on line 5. Every time you set the Student's field values, you overwrite the previous values with the new ones. That's why in the end, after "adding" what you thought was three students (when in fact you just kept overwriting the same ONE student), you only see the last student values because all three Nodes refer to the same Student object!

You need to move line 5 inside your loop. This way, you'll get a different student object from what you created in the previous iteration.
+Pie Number of slices to send: Send
Thank you Junilu! What an oversight from my part! Still learning how queues work with nodes.
+Pie Number of slices to send: Send
The problem wasn't so much how queues work with nodes as it was misunderstanding how object references work. On line 27 in main() you pass newStudent to the enqueue() method. Since the reference you passed was never assigned to a different Student instance, you ended up with all nodes that you added to the queue pointing to the same Student object that was created on line 5. The same thing would have happened if you were working with Lists or Maps.
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1305 times.
Similar Threads
null pointerException handling
Spotting errors
Issue with the way my program is processing input and output
A continuation of my previous post, if I may...
Inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:23:07.