0

i am writing a a program that has two classes. the first class has an array which will be able to add the other class into its array with a length of 10. however i get the error saying "Cannot find Symbol". so my question is how do i add a seperate class to the array of an another class?

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Arrays;

public class HomeInventoryManager

private int[] listOfInventoryItems;
/**
 * Initialise the home inventory manager.
 */

public HomeInventoryManager()
{
    listOfInventoryItems = new int[10];
    InventoryItem = 0;  **i get the error here saying it cannot find the symbol variable InventoryItem** 
}

public void addInventoryItem()
{
    listOfInventoryItems[InventoryItem] = anInt;
    inventoryItem++;
}

UML diagram

3
  • 1
    Whether the inventoryItem an int or a separate class? Commented Apr 23, 2016 at 2:48
  • 1
    it is a separate class Commented Apr 23, 2016 at 3:02
  • 1
    Can you put the code of inventory Item. Otherwise can't tell the exact solution. In short, you should call a method in that class to increment the inventoyItem count. Commented Apr 23, 2016 at 3:23

2 Answers 2

1

That's obvious. You haven't declared what is inventoryItem. It whould be declared at the top of the class similar to the declaration of listofInventoryItems

public class HomeInventoryManager{

    private int[] listOfInventoryItems;
    private int inventoryItem;
    /**
     * Initialise the home inventory manager.
     */
    public HomeInventoryManager()
    {
        listOfInventoryItems = new int[10];
        inventoryItem = 0;
    }
    //your code here
}

Also, in the other 2 methods, you have 2 spellings for inventoryItem. Correct it also.

Sign up to request clarification or add additional context in comments.

1 Comment

the inventoryitem item is a separate class though
0
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Arrays;

public class HomeInventoryManager{


private int[] listOfInventoryItems;
private int InventoryItem;

//constructor
public HomeInventoryManager()
{
    listOfInventoryItems = new int[10];
    InventoryItem = 0;
}

public void addInventoryItem()
{
    listOfInventoryItems[0] = anInt; 
    inventoryItem++;
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.