aspose file tools*
The moose likes Beginning Java and the fly likes Objects attributes accessing from a hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects attributes accessing from a hashtable" Watch "Objects attributes accessing from a hashtable" New topic
Author

Objects attributes accessing from a hashtable

Alex Garci
Greenhorn

Joined: Feb 14, 2012
Posts: 15
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.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Using a Venta object. Seems you already have the Venta objects in the below array.
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
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

Joined: Feb 14, 2012
Posts: 15
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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?
 
jQuery in Action, 2nd edition
 
subject: Objects attributes accessing from a hashtable