• Post Reply Bookmark Topic Watch Topic
  • New Topic

toString() using queues with nodes  RSS feed

 
Adrian Meneses
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. 
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the loop in your demo class?  Can you make sure you posted the same classes that you tested?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adrian Meneses
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adrian Meneses
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu! What an oversight from my part! Still learning how queues work with nodes.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!