How to print each element of an object in an ArrayList

Put your programming skills to the test in these challenges.

How to print each element of an object in an ArrayList

Post by ghostheadx2 on Mon Jun 29, 2015 6:26 pm
([msg=88728]see How to print each element of an object in an ArrayList[/msg])

So, I have a method I want to call that would print every element in an object. It works for one object, if I call the method. However, if I try to call the method in a for-loop, to print each element of each object in an ArrayList, it just prints the memory allocation. Could someone give me a hand at that? Here is what the for-loop looks like:

Code: Select all
for (int i = 0; i < inventoryList.size(); i++){
               System.out.println(inventoryList.get(i).toString());
            }



Here is what the method looks like:

Code: Select all
   public String printProduct(){
      return "Name: "+productName + " ID: " + id + " Quantity: " + quantity + " Price: " + price;
   }


Here is what the whole code looks like:

Code: Select all
import java.util.*;
import java.util.Scanner;

class Inventory{
   public static void main(String[] args){
      Scanner input = new Scanner(System.in);
//Here are the instructions for the user:
      ArrayList<Product> inventoryList = new ArrayList<Product>();
      int cmd=0;
      while (cmd!=5){
         System.out.println("Welcome to Inventory. Here are your 4 options: \n");
         System.out.println("1. See all products stored");
         System.out.println("2. Look up a product by ID");
         System.out.println("3. Add a new product to your inventory");
         System.out.println("4. Take away a product from your inventory");
         System.out.println("5. Exit the program \n");
         System.out.println("Type the number of the option you'd prefer: ");

         cmd = input.nextInt();
//allows the user to enter in commands
     
         if (cmd==1){
            for (int i = 0; i < inventoryList.size(); i++){
               System.out.println(inventoryList.get(i).toString());
            }
         }
         else if(cmd==2){
         }
         else if (cmd==3){
            // add new product to ArrayList
            System.out.println("Enter the name of the product: ");
            String name = input.next();
            System.out.println("Enter the product ID: ");
            int ID = input.nextInt();
            System.out.println("How many of this product do you have: ");
            int quantity = input.nextInt();
            System.out.println("What is the price of this product: ");
            double price = input.nextDouble();
           
            Product aProduct = new Product();
            aProduct.setName(name);
            aProduct.setID(ID);
            aProduct.setQuantity(quantity);
            aProduct.setPrice(price);
            System.out.println(aProduct.printProduct());
            inventoryList.add(aProduct);
            }
         else if (cmd==4){
         }
         else{
            //obviously, in case the user is retarded
            System.out.println("Not a real command. Try again.");
         }
         
      }
   }
}

class Product{
   private String productName;
   private int id;
   private int quantity;
   private double price;
   
   public void setName(String name){
      productName = name;
   }
   
   public String getName(){
      return productName;
   }
   
   public void setID(int ID){
      id = ID;
   }
   
   public int getID(){
      return id;
   }
   
   public void setQuantity(int quant){
      quantity = quant;
   }
   
   public int getQuantity(){
      return quantity;
   }
   
   public void setPrice(double productPrice){
      price = productPrice;
   }
   
   public double getPrice(){
      return price;
   }
   public String printProduct(){
      return "Name: "+productName + " ID: " + id + " Quantity: " + quantity + " Price: " + price;
   }
}


The whole code worked perfectly up until I tried to print the elements of each object in the ArrayList by calling the method. I know its a logic error because it compiles perfectly.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: How to print each element of an object in an ArrayList

Post by tremor77 on Tue Jun 30, 2015 11:15 pm
([msg=88742]see Re: How to print each element of an object in an ArrayList[/msg])

Should note that you got this working... i was working on a response then just happened to see the other thread:

https://www.hackthissite.org/forums/viewtopic.php?f=36&t=12425&sid=3b65445124b2a44c1e6928f2ef02b1f3

:o
User avatar
tremor77
Addict
Addict
 
Posts: 1098
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)


Re: How to print each element of an object in an ArrayList

Post by PraetG on Sun Dec 13, 2015 8:01 am
([msg=90921]see Re: How to print each element of an object in an ArrayList[/msg])

Besides: You do not have to call "toString()", java does so automatically...
PraetG
New User
New User
 
Posts: 3
Joined: Sun Dec 13, 2015 7:45 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests