File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Objects attributes accessing from a hashtable

Alex Garci

Joined: Feb 14, 2012
Posts: 15

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?

John Jai

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

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

Joined: Feb 14, 2012
Posts: 15
Thanks for replying Michael. But I tried it what you just typed and I displayed the following:


I don't know why. The result would be 1, because is what I typed.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46388
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?
I agree. Here's the link:
subject: Objects attributes accessing from a hashtable
It's not a secret anymore!