2

So here is my questions. I am working on building an array using a defined class. The class looks like below:

 class InventoryItem {

    String ItemName;
    int ItemNumber;
    int InStock;
    double UnitPrice;
    double InventoryValue;

    public InventoryItem(String ItemName, int ItemNumber, int InStock, double UnitPrice) {
        this.ItemName = ItemName;
        this.ItemNumber = ItemNumber;
        this.InStock = InStock;
        this.UnitPrice = UnitPrice;
        this.InventoryValue = UnitPrice * InStock;

    }

    public void output() {
        System.out.println("Item Name = " + ItemName);
        System.out.println("Item Number = " + ItemNumber);
        System.out.println("In Stock = " + InStock);
        System.out.println("Item Price = $" + UnitPrice);
        System.out.println("Item value of stocked items = $" + InventoryValue);

    }
}

This works just fine when I load it with a test example.

However when I try to build the array using this class something breaks.

import java.util.*;

public class Inventory {

    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        System.out.println("Welcome to the inventory program\n")
        System.out.println("This will help keep track of your inventory of office supplies");

        InventoryItem[] Stock;
        InventoryItem[10] = Stock;
        Stock[1] = new InventoryItem("Test", 123456, 500, .99);
        System.out.println("Please enter the Item Nmae");
        ItemName = user_input.nextline();

the problem I am having is that it doesn't like the InventoryItem[] Stock; It keeps telling me that it cannot find InventoryItem. I thought that I could use this class as the array basically. I think that I am asking the right questions, but I am new to this so please bear with me. BTW this test example does not work. However this is the initial test example.

 public static void main(String[] args) {
   InventoryItem Stock = new InventoryItem("TEST", 123456789, 999, 1.25);
    Stock.output();

Which works fine to load that one line, but I am working towards having the user input each section of the array in order to load it up. Does that make sense or do I just sound like an idiot? I am not great with the terminology, but I am working on it. Any help would be appreciated.

2
  • If it can't find InventoryItem, it may be that InventoryItem is in the wrong directory, or needs to be imported. So where is the InventoryItem file? Also, it's good practice to make your classes public, unless you have a good reason not to. Commented Nov 10, 2013 at 15:05
  • I might also point out that .nextline(); is not a Scanner method, but .nextLine() is (in ItemName = user_input.nextline();). Also, ItemName has not been declared yet, so you might want to change that to String ItemName = ... unless you've declared it somewhere else. Otherwise, the program doesn't know what ItemName is, and it's not known that it is a variable and is going to store a String. You also seem to be missing a semicolon at the end of the "Welcome" message on line 3 of main(). Commented Nov 10, 2013 at 15:15

3 Answers 3

2
InventoryItem[] Stock;
InventoryItem[10] = Stock;

Should be

InventoryItem[] Stock = new InventoryItem[10];

Also, I see you doing this (which probably doesn't cause a problem, but just an FYI in case you don't know.

 Stock[1] = new InventoryItem("Test", 123456, 500, .99);

Arrays' indices are zero-based, meaning that start at 0. So if you're trying to access the first index with the code above, you're actually accessing the second index.

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

Comments

1

Use new to allocate space for your Stock array of InventoryItem

InventoryItem[] Stock = new InventoryItem[10];

Also, I notice you are using array index 1 to add something to your Stock:

Stock[1] = new InventoryItem("Test", 123456, 500, .99);

This is fine, but if you want it to be in the first position, know that Java arrays are zero based.

See Oracle's tutorial on Java arrays for more information.

Comments

1

Use this instead of brackets:

ArrayList<InventoryItem> Stock = new ArrayList<InventoryItem>();

adding items:

Stock.add(new InventoryItem(/*parameters*/);

You can find the rest of the methods you can use here

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.