This discussion is archived
1 Reply Latest reply: Aug 7, 2012 7:16 PM by 792849 RSS

Objects attributes accessing from a hashtable

927057 Newbie
Currently Being Moderated
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:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;

public class LecturaEscrituraObjetos implements Serializable 
{
     private static final long serialVersionUID = 1L;
     int l=100; //es la longitud del array tha
     Venta[] tha=new Venta[l]; 
     String fichero="Ventas.dat";

        transient Scanner s=new Scanner(System.in);
     Hashtable<String, Venta> th=new Hashtable<String,Venta>();
     
     LecturaEscrituraObjetos()
     {
          for (int j=0;j<tha.length;j++)
          {
               tha[j]=new Venta();
               tha[j].idv=" ";
               tha[j].precio=0;
               tha[j].unidades=0;
          }
     }

        public void guardar_venta(Venta v) throws IOException //guarda en el hashtable el objeto venta
     {
          boolean exst=false; //variable booleana para determinar si existe la clave de la venta en th
          
          
          InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader (isr);
          
       
        
               try
                  {
                 System.out.println("Crear venta");
             
                 System.out.println("Número de unidades");
                 v.unidades=s.nextInt();
                 System.out.println("Precio de la venta");
                 v.precio=s.nextFloat();
                 while(!exst)
                    {
                       System.out.println("Clave de la venta");
                       v.idv=br.readLine();
                       if(!th.containsKey(v.idv)) 
                               {
                              th.put(v.idv,v);
                              System.out.println("Venta creada correctamente con código " + v.idv);
                              exst=true;
                               }
                             else 
                                 {
                                System.out.println("Esta clave ya existe");
                                 }
                  }
                       }
        
                  catch (Exception e)
                    {
                      System.out.println("Error al leer de teclado");
                   e.printStackTrace();
                   System.out.println(e);
                    }
                                
     }

        System.out.println(th.values());
        System.out.println(th.toString());
        System.out.println(th.elements()); 
}
import java.io.*;
import java.util.Scanner;

public class Venta implements Serializable
{
     private static final long serialVersionUID = 1L;
     String idv;
     int unidades;
     float precio;
     transient Scanner s=new Scanner(System.in);

        Venta()
     {
          
     }
}
How can I acess to the Venta attributes?

Thanks.
  • 1. Re: Objects attributes accessing from a hashtable
    792849 Newbie
    Currently Being Moderated
    You just need to override the toString method... like this:
    public class Venta implements Serializable
    {
         private static final long serialVersionUID = 1L;
         String idv;
         int unidades;
         float precio;
         transient Scanner s=new Scanner(System.in);
     
            Venta()
         {
              
         }
            
            public String toString() {
                    return "idv=" + idv + ", unidades=" + unidades + ", precio=" + precio;
            }
    }

    Edited by: thiagovespa on Aug 7, 2012 7:15 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points