Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Im a new to Java.... I am trying to do a program where user enters details of a product and then I store them in an array so that when the user wants, he can see the products listed. However, I am finding it hard to see how to do the array. Can someone please take a look at the code and explain what I did wrong?


Thank you in advance

What I have tried:

import java.util.Scanner;

public class pr {

public static void main (String[] args){
    

      Scanner in = new Scanner (System.in);
      
       int cost, sell, stock, vat;
       String description, id;
      
      String [] productDetails = "not";
      
      System.out.println("___________________");
      
      System.out.print("Enter product's unique identification number: ");
      productDetails[0] = in.next();
      System.out.print("Enter product's description: ");
      productDetails[1] = in.next();
      System.out.print("Enter product's costing price: ");
      productDetails[2] = in.toString();
      System.out.print("Enter product's selling price: ");
      productDetails[3] = in.toString();
      System.out.print("Enter quantity of the product: ");
      productDetails[4] = in.toString();
      System.out.print("Enter VAT cost of product: ");
      productDetails[5] = in.toString();
      
     
     
      System.out.print(productDetails);
     
     }
 }

Posted
Updated 26-Nov-18 17:53pm

1 solution

You should create a class like this
This class will describe the details of the property of each product
class ProductDetails {
  public String idNo;
  public String description;
  public double cost;
  public double price;
  public int Quantity;
  
}


Declare an array of ProductDetails
ProductDetails[] pdr = new ProductDetails[n];


Run the for loop on this
for(int i=0;i<n;i++) {
  pdr[i]=readEachProductDetails(); 
}


You also need to define
read_each_product_details

ProductDetails readEachProductDetails () {
 ProductDetails productDetails = new ProductDetails();
  // TODO your code to read the product details
 return productDetails;
}


And assemble all this together
 
Share this answer
 
Comments
Member 14066606 27-Nov-18 9:36am    
when I do the readEachProductDetails(); it is giving me an error of : cannot find the method symbol
Mohibur Rashid 27-Nov-18 17:51pm    
Have you declared it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900