Member Avatar for coil

OK, the problem is you declared a Scanner input twice...so get rid of the Scanner input; line in the main method - you already declared it! Also get rid of the line right underneath it where you initialize the Scanner - you already did that too.

Hello sorry about that, here is my entire program code, I am getting an error, the input is underlined error on NetBeans. I must not be implementing something accurately still. My camera class is complete, just the Inventory class I am having a hard time with.

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
// 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

        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

Please put your code in code tags. Use the icon to right above input field.

Member Avatar for coil

What is the error? I actually copied your code and compiled it myself and it works fine.

You did coil? hmm. There are no errors displayed in NetBeans. Should I try to run in through the cd? How does this look? can you post the exact code you copied of mine? So I can copy it and save it just in case. Thanks

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

import java.util.*;

public class Inventory 
{
   
private static 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) {
        Inventory.input = input;
    }
  
} // end class main
Member Avatar for coil

Here it is. Note that I took out the private constructor in the Inventory class. You don't need it, but it doesn't cause an error if you put it back in.

If it doesn't cause errors in the IDE, then it should work. Now check for logic errors - does the program do what you want it to do?

import java.util.*;

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

public class Inventory 
{
   
    private static 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
      // 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

        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

 
} // end class main

Hello here is my entire code, I have not tried to run the program using the cd. I am going to try right now. This code that I posted compiles for and runs for you? NetBeans is sensitive I take it, I tried to run a test and it failed.

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

import java.util.*;

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

    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) {
        Inventory.input = input;
    }
} // end class main


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

Hello, I got my code to compile Thanks alot coil you dont know how important this is for me. Takes a good person to help another thanks. I am going to be working on part 2 of my Inventory Program, which I am going to be required to implement an array to store information, can you help me with that in another day or two?

Member Avatar for coil

Sure, not problem. If your code so far works, the second part should be pretty easy.

Hello, I got my Inventory class to compile, but not my camera class you wouldnt happen to know why? or is this part 2 of the program mod?. Part 2 of the Inventory requires me to implement an array.

Here are the phase two instructions:

•Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information about one product at a time, including the item number, the name of the product, the department of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

• Create a method to calculate the value of the entire inventory.

• Create another method to sort the array items by the name of the product.

got my Inventory class to compile, but not my camera class

Please copy and paste the FULL text of the error messages here.

Member Avatar for coil

As Norm said, yeah, everytime you get an error, post the relevant code + error messages with the lines at which they occur. Otherwise, I can only randomly guess at what the error is.

For part 2, here would be my suggestions:
1. I would assume it would require more classes...e.g. Camera, TV, Computer. If so, code the classes based on your camera class, and add the required methods to your classes.

2. If you have a getValue() or similar method in all of your item classes that returns the total value of all the units of that item, this should be very simple.

3. This is a bit harder, but for starters, ensure you have a getName() method.

Hello coil, can you give me a brief example of how I would declare the getvalue and getName methods to store data and product out number of units times the unit price. Do I declare these methods in the Inventory class?

Here is my second part of the Inventory Program, how is it looking so far? Main method is underlined as error. Anyone know why? Also what is the best way to post a precise error?

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

import java.util.*;

public class Inventory {
    //declare a scanner to take input from the console.

    private static Scanner input = new Scanner(System.in);

    public static void main(String args[], Camera[] Camera) {
        // main methods begins execution of java application

        Camera myCamera = new Camera("Sony", 45, 20, 200.0);
        Camera[1] = new Camera("Philips", 0, 0, 0.0);
        Camera[2] = new Camera("Toshiba", 0, 0, 0.0);
        for(int i = 0; i < 0; i++) { 
            
        } 
        System.out.println("Camera Inventory Program\n");
        System.out.println("Product Name is\n " + myCamera.getProductName());
        System.out.println("\nItem Number is\n" + myCamera.getItemNumber());
        System.out.println("\nNumber of Units is\n" + myCamera.getNumberofUnits());
        System.out.println("\nUnit Price is\n" + 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) {
        Inventory.input = input;
    }
} // end class main

// Fig.: 2.3 Inventory.java
// Program displays product of Number of Units and Unit Price

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 Sony, int Number, int Units, double Price) {
        // main methods begins execution of java application


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

    } // end four-argument constructor

    // set Department Name
    public void setProductName(String Name, String Sony) {
        ProductName = "Sony";
    }

    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
Member Avatar for coil

The reason main is triggering an error is because the parameter for main must be (String[]args). You cannot have additional parameters.

To post errors, tell us the error (e.g. NullPointerException), and the exact line of code that it occurs (if it's in a loop or something, give us the surrounding few lines for context). Alternatively, you could post exactly what you see on the console.

I would suggest not making Inventory the main class. A class hierarchy as follows might be better.

-class Camera
-class Inventory <- You can instantiate this class

-main class (with the main method) StorageProject (or any other name) <- This is the new "main" class with the public static void main method

As for getValue() and getName(), you need to make a decision: do you want to have the Inventory class store multiple Camera objects, each representing one camera, or do you want the Inventory class to store one Camera object, representing multiple cameras?

Pseudo-code:

class Camera {
	...
	public String getName() {
		return "Camera";
	}
	public int getValue() {
		return <unit price>;
	}
	//Note that if you decide to have Camera represent multiple cameras, you will need an additional 	
	getTotalValue() method.
}

class Inventory {
	...
	public int getValue() {
		return <# of camera units> * <camera>.getValue() //If Inventory stores multiple cameras
		//Otherwise...
		return <camera>.getTotalValue() //If Inventory stores 1 camera
	}
}

Hello coil, thank you for the feedback. I apologize I am a little confused on how I would implement these methods in an array to store and display the data. How would I declare Inventory as not a main class? Camera class is my main class? When i try to run my program says class Inventory and class Camera do not contain a main method. Should I keep all my code in one class? Under my default packages I have two classes Camera and Inventory, am I supposed to have one main class?

Here is my current code so far, I have trouble locating the proper section of the code to modify the methods for an array.

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

import java.util.*;

class Inventory {
    //declare a scanner to take input from the console.

    private static Scanner input = new Scanner(System.in);

    public static void Camera(String[]args, Camera[] Camera) {
        // Camera methods begins execution of java application

        Camera myCamera = new Camera("Sony", 45, 20, 200.0);
        Camera[1] = new Camera("Philips", 0, 0, 0.0);
        Camera[2] = new Camera("Toshiba", 0, 0, 0.0);
        for(int i = 0; i < 0; i++) {

        }
        System.out.println("Camera Inventory Program\n");
        System.out.println("Product Name is\n " + myCamera.getProductName());
        System.out.println("\nItem Number is\n" + myCamera.getItemNumber());
        System.out.println("\nNumber of Units is\n" + myCamera.getNumberofUnits());
        System.out.println("\nUnit Price is\n" + 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 Camera method

    private Inventory() {
    }

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

    /**
     * @param input the input to set
     */
    public void setInput(Scanner input) {
        Inventory.input = input;
    }
} // end class Camera

// Fig.: 2.3 Inventory.java
// Program displays product of Number of Units and Unit Price

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 Sony, int Number, int Units, double Price) {
        // main methods begins execution of java application


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

    } // end four-argument constructor

    // set Department Name
    public void setProductName(String Name, String Sony) {
        ProductName = "Sony";
    }

    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 getValue() {
        return UnitPrice * NumberofUnits;

    } // end method Camera
} // end class Camera
Member Avatar for coil

Sorry, I wasn't very clear.

What I meant is, right now, Camera is a class that represents the object Camera. I propose making Inventory another class, that represents the object Inventory. To do this, choose a name other than Camera and Inventory that represents your project; I'll use the name InventoryProject. You can choose anything you want, just replace InventoryProject with your choice.

1. Create a new file/project InventoryProject.java

2. If you're using NetBeans or some other IDE, a class should already be created for you, that'll look like this:

public class InventoryProject {
	public static void main(String[]args) {
	}
}

This is what I will refer to as the main class - it is the only class with the public static void main method.

3. Copy in the above code that you just posted

Now you'll still get errors. I recommend keeping your Camera class intact, but completely re-writing your Inventory class.

Here is something to get you started.

class Inventory { //The Inventory class
	public Inventory() { //This is the constructor - note that it is public
	}
	//Your methods
}

Don't put a Scanner in the Inventory class. Put it in the InventoryProject (main) class.

Hope this helps!

Thats ok coil, thank you for the feedback, I am a little more clean now, I created a new file class, under default packages I have a Camera class, Inventory class, and InventoryProject class. DO i need to delete a class? My code is showing few errors in the class loader. my code isnt showing any errors, but the class loader is showing the errors I posted belowe, my program is conflicting with the test package as well. Do I need to implement a test package, or does NetBeans do this automatically?

Error 1    throw new ClassNotFoundException(name);
			}

Error 2    return (Class)


Error 3    c = findClass(name);

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

Sorry, can you clarify what you mean by classloader? As in, when are you getting these errors? Compile time, run time, etc.

I wouldn't worry about packages for now.

Hello coil, thank you for the feedback, I have been working on the second phase of my Inventory Program. Under my default packages I have 3 classes, Camera.java, InventoryProject.java, and Inventory.java. Do I only need one main class? Do I need to delete a class? The classloader specifically informs the programmer of the errors.

Member Avatar for coil

This is how classes work: in each .java file, you should have one public static void main method, and only one. That method should be within a class that has the same name as the .java file name. You can have other methods in that class, but keep the main method header the same.

In other words, if I have a file named InventoryProject.java:

//As many classes as you want here

public class InventoryProject { //Name must be same as file name
	//As many methods as you want here

	public static void main(String[]args) { //This must be exactly as it is here - do not modify
		//Add any code you want here
	}
}
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.