0 votes

What is the best way to create a method to add a product in an inventory class in Java, inside a vector?

I have a class called RegistroInv in java, which is designed to contain the arrays of the Products class containing the respective attributes, which must be linked later with a one-dimensional array and a matrix of 5 columns and 10 rows. Trying to create the respective method to add a product, in order to store it in the vector, the compiler rejects me the code. I have:

import java.util.Random;
public class RegistroInv{

    public Productos producto[];
    public int tablaProductos[][];

    //Constructor de la clase
    public RegistroInv(){
        producto = new Productos [5];
        tablaProductos = new int [10][5];

    //Codigo para agregrar productos
    public boolean addProducto (Productos productos){
        int i=0;
        boolean realizado=false;

            for ( i=0; i<producto.length; i++){


                producto [i] = productos ;


            }//Cierra ciclo

        return realizado;

    }//Cierra metodo agregar

}//Cierra la clase

I do not understand what the compiler is telling me. I attach the code of the original object Products.java:

public class Productos{

    private String id;
    private String costo;
    private String marca;

    //Constructor 1
    public Productos (String id, String costo, String marca){

        setid (id);
        setCosto (costo);
        setMarca (marca);

    //Metodos para la clase principal
    public void setid (String id){
        this.id = id;

    public String getid(){
        return id;

    public void setCosto (String costo){
        this.costo = costo;

    public String getCosto (){
        return costo;

    public void setMarca (String marca){
        this.marca = marca;

    public String getMarca (){
        return marca;

    public String toString(){
        return "Id de producto: "+getid()+"\nPrecio del producto: "+getCosto()+"\nMarca registrada: "+getMarca();

Error: RegistroInv.java:18: error: array required, but Productos found introducir la descripción de la imagen aquí


Antonio Miranda Points 31
if(producto [i] = 0) {
    producto [i] = productos;

There are a couple of things wrong with this block:

  • Within the if you are expecting something boolean, and you are doing an assignment (=). Substitute the = by == to compare product [i] with 0.

  • However, product [i] is of type Products and you are comparing it with a integer .

If you want to compare a particular product field [i] with 0, you must implement the comparator or access the field you want to compare by using the getters that you have implemented (note that the Product fields are Strings ). It would look something like this:

if(Integer.parseInt(producto [i].getid()) == 0) {
    producto [i] = productos;
    realizado = true;

These are the small syntax errors I see, although I don't quite understand the functionality....



HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: