0

I'm trying to create an Inventory System with a GUI using JSwing and JAVA-WS in Eclipse and I am stuck on trying to turn the Hash into an array. I am in no way an expert in Java, so I came here to ask for help.

Here's the code:

@Override
public InventoryItem [] getAllInventoryItem() {
    ArrayList<InventoryItem> items = new  ArrayList<InventoryItem>();
    InventoryItem item=null;
    
    for(Map.Entry<String, InventoryItem> e : inventories.entrySet())
    {
        item=new InventoryItem();
        item.setBrand(e.getValue().getBrand());
        item.setCode(e.getKey());
        item.setName(e.getValue().getName());
        items.setPrice(e.getValue().getPrice());
        
        items.add(item);
    }
    return (InventoryItem []) items.toArray(new InventoryItem[items.size()]);
}

But when I call the method:

System.out.println((inventory.getAllInventoryItem()));

This is what comes out:

[Lcom.lansang.inventory.fx.InventoryItem;@123772c4

I also tried:

System.out.println((Arrays.toString(inventory.getAllInventoryItem())));

But still, I dont get the elements inside the array. It only returns this:

[com.lansang.inventory.fx.InventoryItem@123772c4, com.lansang.inventory.fx.InventoryItem@2d363fb3]
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 2
    That is just how objects print out if you have not overridden the `toString` method - see [here](https://stackoverflow.com/a/3615757/2670892). Your InventoryItem needs a toString method. – greg-449 Jun 26 '22 at 09:23
  • You can follow this article to know about [How to Override toString Method](https://www.geeksforgeeks.org/how-to-override-tostring-method-for-arraylist-in-java/) – Sayan Bhattacharya Jun 26 '22 at 12:10
  • I did overwrote it now on my InventoryItem Class: with: public String toString(){ return code+" "+ name +" "+ brand + "" + price; } but it seems that my client class for some reason doesn't detect the changes I added and still prints out hexes. But thanks for the help! I've got something to worked with now. – Ckxsl023 Jun 27 '22 at 03:11

0 Answers0