1

I re-type these code from a book and somehow I got error " Cannot find symbol - class InventoryItem "

import java.util.Scanner;

public class ReturnObject {    
    public static void main(String[] args) {
        InventoryItem item;
        item = getData();

        System.out.println("Des: " + item.getDescription() + " Unit: " + 
        item.Units());

    }

    public static InventoryItem getData() {
        String desc;
        int units;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("enter descri: ");
        desc = keyboard.nextLine();
        System.out.print("number of unit: ");
        units = keyboard.nextInt();
        return new InventoryItem(desc, units);
    }
}

I'm new to java please help thank you.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Đức Seven
  • 57
  • 1
  • 5

3 Answers3

0

The class you are currently in cannot find the class (symbol) InventoryItem. You need to define this class & the getData method.

public class InventoryItem{

    private String desc;
    private int units;

    public InventoryItem(){
        Scanner keyboard = new Scanner(System.in);
        System.out.print("enter descri: ");
        desc = keyboard.nextLine();
        System.out.print("number of unit: ");
        units = keyboard.nextInt();

    }

    public static InventoryItem getData() {

        return this;
       }

    }
Matthew
  • 817
  • 1
  • 13
  • 39
0

I think this should be the InventoryItem you need.

 /**
 * This class uses three constructors.
 */

public class InventoryItem {
    private String description;  // Item description
    private int units;           // Units on-hand

    /**
     * No-arg constructor
     */

    public InventoryItem() {
        description = "";
        units = 0;
    }

    /**
     * The following constructor accepts a
     * String argument that is assigned to the
     * description field.
     */

    public InventoryItem(String d) {
        description = d;
        units = 0;
    }

    /**
     * The following constructor accepts a
     * String argument that is assigned to the
     * description field, and an int argument
     * that is assigned to the units field.
     */

    public InventoryItem(String d, int u) {
        description = d;
        units = u;
    }

    /**
     * The setDescription method assigns its
     * argument to the description field.
     */

    public void setDescription(String d) {
        description = d;
    }

    /**
     * The setUnits method assigns its argument
     * to the units field.
     */

    public void setUnits(int u) {
        units = u;
    }

    /**
     * The getDescription method returns the
     * value in the description field.
     */

    public String getDescription() {
        return description;
    }

    /**
     * The getUnits method returns the value in
     * the units field.
     */

    public int getUnits() {
        return units;
    }
}

complete example click here and here

xxy
  • 1,058
  • 8
  • 14
0

maybe your InventoryItemclass:



    public class InventoryItem {
        private String desc;
        private int units;
        public InventoryItem(String desc, int units) {
            this.desc=desc;
            this.units=units;
        }

        public String getDescription() {
            return desc;
        }

        public int Units() {
            return units;
        }
    }