• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

toString() using queues with nodes

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.  
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic