Hello, I am developing an inventory program in one of my IT classes. So far I have implemented my Camera class correctly, I just need some help configuring my Inventory class. I have underlined the section of my code I am having trouble with. If someone could take a peek and help me out, I would greatly seriously appreciate it. Thanks

class Camera {
    // create Scanner to obtain input from command window


    private String ProductName; // Product Name
    private int ItemNumber; // Item Number
    private int NumberofUnits; // Number of Units
    private double UnitPrice; // Unit Price


    // initialize four-argument constructor
    public Camera(String Name, int Number, int Units, double Price) {


        ProductName = Name; // validate and store the Product Name
        ItemNumber = Number; // validate and store the Item Number
        NumberofUnits = Units; // validate and store the Number of Units
        UnitPrice = Price; // validate and store the Unit Price

    } // end four-argument constructor

    // set Department Name
    public void setProductName(String title, String Name)
    {
        ProductName = Name;
    }
    public String getProductName() {
        return ProductName;
    }

    public void setItemNumber(int Number) {

        ItemNumber =  Number;
    } 
    public double getItemNumber() {
        return ItemNumber;
    } 

    public void setNumberofUnits(int Units) {
        NumberofUnits = Units;
    } 
    public double getNumberofUnits() {
        return NumberofUnits;
    } 

    public void setUnitPrice(double Price) {
        UnitPrice = Price;
    }

    public double getUnitPrice() {
        return UnitPrice;
    } 

    public double value() {
        return UnitPrice * NumberofUnits;
    }

    } // end class Camera


public class Inventory 
{
    // main methods begins execution of java application
    // instantiate cars object
    public static void main( String args[]) 
   {

    Inventory myCamera = new Inventory( "Sony");
    {
    System.out.println("Camera Inventory Program");    
    System.out.printf("Product Name is");
        myCamera.getProductName());  
    System.out.printf("Item Number is"); 
        myCamera.getItemNumber() );
    System.out.printf("Number of Units is");
        myCamera.getNumberofUnits() );
    System.out.printf("Unit Price is"+
        myCamera.getUnitPrice());

    System.out.println("\n\nEnter Product Name or enter stop to quit:");//prompt
    // myCamera.setProductName(input.next()); 
    // read Product Name or stop 


       }
} // end main method



} // end class Inventory

Recommended Answers

All 50 Replies

Member Avatar for coil

The first problem, at the line Inventory myCamera = new Inventory( "Sony"); is that you can't instantiate a class like that. What exactly are you trying to do there?

The other problem is that your semicolons are in the wrong position and you have too many brackets.

Instead of this:

System.out.printf("Product Name is");
myCamera.getProductName());

Do this:

System.out.printf("Product Name is " + myCamera.getProductName());

The + operator is overloaded for Strings and it concatenates.

Hello, first I must say thank you for the feedback, I made some changes but I still cant get the program to compile.

public class Inventory
{
// main methods begins execution of java application

public static void main( String args[])
{

System.out.println("Camera Inventory Program");
System.out.printf("Product Name is " + myCamera.getProductName());
System.out.printf("Item Number is" + myCamera.getItemNumber() );
System.out.printf("Number of Units is" + myCamera.getNumberofUnits() );
System.out.printf("Unit Price is" + myCamera.getUnitPrice());


System.out.println("\n\nEnter Product Name or enter stop to quit:");//prompt
// myCamera.setProductName(input.next());
// read Product Name or stop

} // end main method

private Inventory() {
}


} // end class Inventory

Hello, first I must say thank you for the feedback, I made some changes but I still cant get the program to compile.

It would be helpful if you could include the compiler output, which is telling you why it won't compile. When you type "javac Inventory.java", what do you see?

It's likely that a programmer can spot things that are causing you a problem, but if you identify the immediate problem, you're likey to get a better answer than if you just say "it's broken".

Also, always use the CODE tags - otherwise, your code is annoying and hard to read.

Okay, standard grumpiness dispensed with, let's have a look.

You've got a private constructor. I suppose I could find a reason for doing that - if you wanted an object that could only be instantiated by itself - but it doesn't seem like it's what you want here. However, I don't see how that should cause it to fail to compile.
You're using printf instead of println, but with println syntax, but that shouldn't be breaking your program at the compiler level.

You have method calls that I don't see referents for here, I assume that myCamera exists and is accessible to this class, and these methods exist in it? If so, I don't see what the problem will be. Give us a hint, why don't you?

public class Inventory 
{
    // main methods begins execution of java application
    
    public static void main( String args[])
   {
   
    
 
    System.out.println("Camera Inventory Program");
    System.out.printf("Product Name is " + myCamera.getProductName());
    System.out.printf("Item Number is" + myCamera.getItemNumber() );
    System.out.printf("Number of Units is" + myCamera.getNumberofUnits() );
    System.out.printf("Unit Price is" +  myCamera.getUnitPrice());
      

    System.out.println("\n\nEnter Product Name or enter stop to quit:");//prompt
    // myCamera.setProductName(input.next());
    // read Product Name or stop


       
} // end main method

    private Inventory() {
    }

  
} // end class Inventory
Member Avatar for coil

Now the problem is you don't instantiate myCamera at all.

Put the following line of code in your program as the first statement in the main method and it should work:

Camera myCamera=new Camera("Name", 0, 0, 0.0);

As for your private constructor, once again, I'm not sure what exactly you want to do. In this case, you don't need that at all.

Now the problem is you don't instantiate myCamera at all.

Ah. Yes, of course. Good eye, coil.
You see how it would help to give us the compiler errors? It's pretty easy to spot that if you know to look for it. Otherwise, it's sort of a crapshoot what any particular reader will see.

Thanks coil your awesome, how does this look?

public class Inventory
{
// main methods begins execution of java application

public static void main( String args[])
{

Camera myCamera=new Camera("Name", 0, 0, 0.0);

System.out.println("Camera Inventory Program");
System.out.printf("Product Name is " + myCamera.getProductName());
System.out.printf("Item Number is" + myCamera.getItemNumber() );
System.out.printf("Number of Units is" + myCamera.getNumberofUnits() );
System.out.printf("Unit Price is" + myCamera.getUnitPrice());


System.out.println("\n\nEnter Product Name or enter stop to quit:");//prompt
// myCamera.setProductName(input.next());
// read Product Name or stop

} // end main method

private Inventory() {
}

} // end class Inventory

Member Avatar for coil

Looks good!

Just a heads-up: although you commented it out, you also refer to a variable "input" in the line myCamera.setProductName(input.next()); . Don't forget to instantiate that too, if you eventually use it.

Hello, thanks a bunch I appreciate your feedback, can you give me an example for how I would implement myCamera.setProductName(input.next());

Looks like you want input to be a Scanner - so declare and instantiate a Scanner object named input, and you're off to the races. You'll be doing no input validation, which means anything the user types in will be passed directly to the setProductName method - but that should be okay, as long as you're not expecting any particular values. (Will it break if they misspell the name of the camera? If so, check the input somewhere)

Hello, once again thank you for your feedback, now the scanner command I have trouble with. Do I declare // program uses a scanner or program uses a class scanner or are these ignored by the comilier? I have not implemented a scanner command is this why my program will not compile?

Hello, thank you for your feedback, I have trouble declaring a proper scanner command is the in the program. Do i need a basic like the one below? or different scanner command. This is is command I have trouble with. If you could help me out on implemeting a sufficient scanner command and getting this program to compile I would greatly appreciate it. Thanks jon

.import java.util.Scanner; // program uses class Scanner

Omit the intitial period and it should work fine.

Hello jon, is theres a special scanner command i need to impelement? Also how would I go about omitting the initial period? Thanks

You omit the initial period by not typing it. You have

.import java.util.Scanner;

it should be

import java.util.Scanner;

This gives you the Scanner, which is an object (not a command). You instantiate the object just as you would any other object, by declaring a variable of type Scanner, and assigning to it the output of Scanner's constructor.
Like so:

Scanner input = new Scanner(System.in);

That is,

Scanner input;   // declare a variable, input, which is of type Scanner
input = new Scanner(System.in); // input is a pointer to the object created by 
                                // running Scanner's constructor with the filestream
                                // System.in as its parameter

Read the API for the Scanner, it'll answer your questions about usage.

Member Avatar for coil

Just one more thing - Scanners can be used for multiple purposes, including reading from files. So, in the future, be careful to pass System.in as the argument.

Hello, I am still a little confused on implementing the scanner object. DO I input the scanner above class Inventory? Is this the scanner I am looking for to compile system.out the data?

This is the scanner object I need to implement?

Scanner input; // declare a variable, input, which is of type Scanner
input = new Scanner(System.in); // input is a pointer to the object created by
// running Scanner's constructor with the filestream
// System.in as its parameter

Member Avatar for coil

I'm not quite sure what you're asking...here's some sample code to help you out:

import java.util.*;

public class Tester {
	public static void main(String[]args) {
		Scanner s=new Scanner(System.in); //declare a scanner to take input from the console
		
		String tmp=s.next(); //assigns the next input to the string tmp
		int tmp2=s.nextInt(); //assigns the next input to the int tmp2; if input is not int, error
	}
}
Member Avatar for coil

Hello, I am still a little confused on implementing the scanner object. DO I input the scanner above class Inventory? Is this the scanner I am looking for to compile system.out the data?

This is the scanner object I need to implement?

Scanner input; // declare a variable, input, which is of type Scanner
input = new Scanner(System.in); // input is a pointer to the object created by
// running Scanner's constructor with the filestream
// System.in as its parameter

Yes, that should work.

Hello, thank you guys for your feedback. Am I close? I am still getting just couple errors, they are not showing underlined on NetBeans but I am still doing something wrong. I can post my specific errors from NetBeans if that will help also, I was wondering do I need to implement setDepartmentName, etc data in order to comeplete the program and compile the program?

import java.util.;

public class Inventory 
{

    public static void Inventory( String args[])

    Scanner input = new Scanner(System.in);//declare a scanner to take input 
    // from the console.
   {
   // Inventory methods begins execution of java application
    Camera myCamera=new Camera("Name", 0, 0, 0.0);

    System.out.println("Camera Inventory Program");
    System.out.printf("Product Name is " + myCamera.getProductName());
    System.out.printf("Item Number is" + myCamera.getItemNumber() );
    System.out.printf("Number of Units is" + myCamera.getNumberofUnits() );
    System.out.printf("Unit Price is" +  myCamera.getUnitPrice());


    System.out.println("\n\nEnter Product Name or enter stop to quit:");//prompt
    // myCamera.setProductName(input.next());
    // read Product Name or stop


} // end Inventory method

    private Inventory() {
    }

} // end class Inventory
Member Avatar for coil

Yes, please post your errors.

As a rule, it's good to provide the errors. I'm looking at this for maybe two minutes, if I see something in that time, I'll let you know. The errors tell me where to look.

Hello, these are the specific errors NetBeans classloader is displaying. It is not showing any errors underlined in my code just two little red ! mark for my import java.util.; and public static void Inventory( String args[]). The bold and underlined coding is where the errors are.

protected Class<?> findClass(final String name)
     throws ClassNotFoundException

throw new ClassNotFoundException(name);

return (Class)
    AccessController.doPrivileged(new PrivilegedExceptionAction() {         public Object run() throws ClassNotFoundException {
        String path = name.replace('.', '/').concat(".class");
        Resource res = ucp.getResource(path, false);
        if (res != null) {

c = findClass(name);

return loadClass(name, false);
Member Avatar for coil

Instead of import java.util.; write import java.util.*; note the asterisk!

Also, instead of public static void Inventory... write public static void main... do not change the name of the main method.

Hello, I got it to compile the first time, second time it is giving me same errors again. Here is my current code.

class Camera {
    // create Scanner to obtain input from command window


    private String ProductName; // Product Name
    private int ItemNumber; // Item Number
    private int NumberofUnits; // Number of Units
    private double UnitPrice; // Unit Price


    // initialize four-argument constructor
    public Camera(String Name, int Number, int Units, double Price) {


        ProductName = Name; // validate and store the Product Name
        ItemNumber = Number; // validate and store the Item Number
        NumberofUnits = Units; // validate and store the Number of Units
        UnitPrice = Price; // validate and store the Unit Price

    } // end four-argument constructor

    // set Department Name
    public void setProductName(String title, String Name)
    {
        ProductName = Name;
    }
    public String getProductName() {
        return ProductName;
    }

    public void setItemNumber(int Number) {

        ItemNumber =  Number;
    } 
    public double getItemNumber() {
        return ItemNumber;
    } 

    public void setNumberofUnits(int Units) {
        NumberofUnits = Units;
    } 
    public double getNumberofUnits() {
        return NumberofUnits;
    } 

    public void setUnitPrice(double Price) {
        UnitPrice = Price;
    }

    public double getUnitPrice() {
        return UnitPrice;
    } 

    public double value() {
        return UnitPrice * NumberofUnits;
    } // end method Camera

    } // end class Camera



[B]// Fig.: 2.3 Inventory.java[/B]
// Inventory program that display's Camera inventory

import java.util.*;

public class Inventory 
{
    // from the console.
     private Scanner input = new Scanner(System.in);//declare a scanner to take input
    // from the console.
    public static void main( String args[])


   {
   // main methods begins execution of java application
    Camera myCamera=new Camera("Name", 0, 0, 0.0);

    System.out.println("Camera Inventory Program");
    System.out.printf("Product Name is " + myCamera.getProductName());
    System.out.printf("Item Number is" + myCamera.getItemNumber() );
    System.out.printf("Number of Units is" + myCamera.getNumberofUnits() );
    System.out.printf("Unit Price is" +  myCamera.getUnitPrice());


    System.out.println("\n\nEnter Product Name or enter stop to quit:");//prompt
    // myCamera.setProductName(input.next());
    // read Product Name or stop


} // end main method

    private Inventory() {
    }

    /**
     * @return the input
     */
    public Scanner getInput() {
        return input;
    }

    /**
     * @param input the input to set
     */
    public void setInput(Scanner input) {
        this.input = input;
    }

} // end class main
Member Avatar for coil

When you initialize your Scanner: private Scanner input = new Scanner(System.in);//declare a scanner to take input add the static modifier so it looks like private static Scanner input = new Scanner(System.in);//declare a scanner to take input What's the purpose of your methods? If you want to set input from a Scanner: String storage=s.next();

Hello, i am experiencing one error. The underlined input is displaying an error. I am trying to compile an inventory program that contains a seperate product class that stores data, such as product name, item number, number of units, and unit price. The inventory class is the section of the code I am having trouble with. I keep getting it down to just couple errors.

// Fig.: 2.3 Inventory.java
// Inventory program that display's Camera inventory

import java.util.*;

public class Inventory 
{
    // from the console.
private static Scanner [B][U]input[/U][/B] = new Scanner(System.in);//declare a scanner to 
    // take input from the console.
    public static void main( String args[])

   {
   // main methods begins execution of java application

        Camera myCamera=new Camera("Name", 0, 0, 0.0);

    System.out.println("Camera Inventory Program");
    System.out.printf("Product Name is " + myCamera.getProductName());
    System.out.printf("Item Number is" + myCamera.getItemNumber() );
    System.out.printf("Number of Units is" + myCamera.getNumberofUnits() );
    System.out.printf("Unit Price is" +  myCamera.getUnitPrice());


    System.out.println("\n\nEnter Product Name or enter stop to quit:");//prompt
    // myCamera.setProductName(input.next());
    // read Product Name or stop


} // end main method

    private Inventory() {
    }

} // end class main
Member Avatar for coil

What's the actual error?

Hello, these are the four errors the classloader is displaying. The underlined bold are the errors.

protected Class<?> findClass(final String name)
     throws ClassNotFoundException
    {
    try {
        return (Class)
        AccessController.doPrivileged(new PrivilegedExceptionAction() 
throw new ClassNotFoundException(name);

c = findClass(name);

return loadClass(name, false);

Hello, above are the classloader errors, here is my current code. The [B][U]input[/U][/B] is yhe only error.

// Fig.: 2.3 Inventory.java
// Inventory program that display's Camera inventory

import java.util.*;

public class Inventory 
{

private static Scanner [B][U]input[/U][/B] = new Scanner(System.in);//declare a scanner to 
    // take input from the console.

    public static void main( String args[])

   {
      // main methods begins execution of java application
      // declare a variable, input, which is of type Scanner
      // input is a pointer to the object created by 
      // running Scanner's constructor with the filestream
      // System.in as its parameter
        Scanner [B][U]input[/U][/B];    
        input = new Scanner(System.in);
        Camera myCamera=new Camera("Name", 0, 0, 0.0);

    System.out.println("Camera Inventory Program");
    System.out.printf("Product Name is " + myCamera.getProductName());
    System.out.printf("Item Number is" + myCamera.getItemNumber() );
    System.out.printf("Number of Units is" + myCamera.getNumberofUnits() );
    System.out.printf("Unit Price is" +  myCamera.getUnitPrice());

    // myCamera.setProductName(input.next());
    // read Product Name or stop


} // end main method

    private Inventory() {
    }

} // end class main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.