Search code examples
javaarraysobjecthashcode

How to return corresponding items as an array in java


In the below code , I'm trying to implement the method getItemByManufacturer which should return the array of items for the manufacturer we test. I have tried to get the array of items for a particular manufacturer but the hashcode is getting printed. Why is that ? I have overridden the toString method too. Any help will be greatly appreciated.

public class Item{
    
    private long serialNumber;
    private String itemName;
    String manufacturer;
    private int warrantyDays;
    private double rating;

Item( String itemName,String manufacturer,long serialNumber,double rating){
        this.serialNumber = serialNumber;
        this.itemName = itemName;
        this.manufacturer = manufacturer;
        this.rating = rating;
    }
    public String toString(){
        return itemName +"\t"+manufacturer+"\t\t"+serialNumber+"\t"+rating;
    }


    public static String[] getItemByManufacturer(Item[] item,String manufacturer){
    String[] matchedItems = new String[item.length];
    for(int i =0; i<item.length;i++){
        if(item[i].manufacturer.equals(manufacturer)){
            matchedItems[i] = item[i];
        }
        else{
            System.out.println("Invalid Input");
        }
    }
    return matchedItems;
}

public class Driver{
    
    public static void main(String[] args){
        
        Item[] item = new Item[5];
        item[0] = new Item("Watch","Dorond",23945357,6.5);
        item[1] = new Item("Sunglass","Niko",13945357,6.5);
        item[2] = new Item("Blender","Decker",33945357,6.5);
        item[3] = new Item("Bottle","Decker",43945357,6.5);
        item[4] = new Item("Bag","WTravel",53965307,6.5);
        
    
   **System.out.println((Item.getItemByManufacturer(item,"Decker")));**
       
       
       
      
        
       }

}

Solution

  • That is because arrays don't have toString method, so you get the hashcode when you print them. A way to print them is by just using an internal java library Arrays.toString:

    System.out.println(Arrays.toString(Item.getItemByManufacturer(item,"Decker"));
    

    or by converting the array to a List with Arrays.asList:

    System.out.println(Arrays.asList(Item.getItemByManufacturer(item,"Decker"));
    

    Moreover, notice that you should change the condition items[i].manufacturer == manufacturer to items[i].manufacturer.equals(manufacturer), strings must always be compared by using the equals method. Also, if the condition matches, you're just returning the entire array, so you won't get the desired result. I suggest this solution, since array are not dynamically sized, is much easier to just return a List:

    public class Item{
        
        private long serialNumber;
        private String itemName;
        String manufacturer;
        private int warrantyDays;
        private double rating;
    
    Item( String itemName,String manufacturer,long serialNumber,double rating){
            this.serialNumber = serialNumber;
            this.itemName = itemName;
            this.manufacturer = manufacturer;
            this.rating = rating;
        }
        public String toString(){
            return itemName +"\t"+manufacturer+"\t\t"+serialNumber+"\t"+rating;
        }
    
    
        public static List<Item> getItemByManufacturer(Item[] items, String manufacturer){
    
            List<Item> matchedItems = new ArrayList<Item>();
            for(int i =0; i< items.length;i++){
                if( items[i].manufacturer.equals(manufacturer)){
                    matchedItems.add(items[i]);
                }
            }
            if(matchedItems.isEmpty()){
                System.out.println("Invalid Input");
            }
            return mathedItems;
        }
    
    public class Driver{
        
        public static void main(String[] args){
            
            Item[] item = new Item[5];
            item[0] = new Item("Watch","Dorond",23945357,6.5);
            item[1] = new Item("Sunglass","Niko",13945357,6.5);
            item[2] = new Item("Blender","Decker",33945357,6.5);
            item[3] = new Item("Bottle","Decker",43945357,6.5);
            item[4] = new Item("Bag","WTravel",53965307,6.5);
            
        
            System.out.println(Item.getItemByManufacturer(item,"Decker"));
           
           
    
       }