• 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

Objects attributes accessing from a hashtable

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have two java classes: LecturaEscrituraObjetos and Venta. The first one is where are the methods to write and read objects from a file and it has the hashtable named th where the objects Venta are saved. The second one is the object that I want to write and read from a file.

There are no errors. The problem is that I don't know the way to access the methods of the object Venta from the hashtable. I tried it using toString(), Values() and Elements() methods but I only see something like Venta@1d009b4. I also tried use the Enumeration interface but I don't know how to copy the elements from the hashtable to the Enumeration.

Here is the code:





How can I acess to the Venta attributes?

Thanks.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a Venta object. Seems you already have the Venta objects in the below array.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Very simple, you put the object into the hash table:



That you do already correctly. Now you can use the key to retrieve your object:



 
Alex Garci
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying Michael. But I tried it what you just typed and I displayed the following:

Venta@17ce4e7

I don't know why. The result would be 1, because is what I typed.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see problems with your Venta class. You have lots of fields, but you do not initialise them in your constructor. You have a Scanner object, which you never use, so you might as well delete it.
You ought to provide a constructor which initialises the Venta object and getXXX methods which allow access to its fields. You should find some similar examples here. All the fields should have private access, so access can only be through the getXXX methods.
As for the output, Venta@17ce4e7, start reading here.

why are you using Hashtable, which is legacy code? Why are you using floats?
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic