Java - Inventory Program Part 2

Error message: ';' expected int Inventory DVDs = new Inventory

Page 1 of 1

7 Replies - 6745 Views - Last Post: 01 February 2010 - 01:41 PM Rate Topic: -----

#1 TracyLB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Java - Inventory Program Part 2

Posted 31 January 2010 - 08:19 PM

I do not understand what I am doing wrong to get this error message: ';' expected int Inventory DVDs = new Inventory[5];
Also I do not understand why my program will not go through the entire array, it only displays the first line in the array. If someone could please tell me what I am doing wrong I would greatly appreciate it.

Thank you!

Here is my code...

//Inventory Program Part 1
//Inventory Class - product class that holds the item number, the name of the product, the number of
//units in stock, and the price of each unit.


public class Inventory
{

 private double ItemNumber; //to store Item number
 private String ItemName; //to store Item name
 private double UnitsInStock; //to store number of units in stock
 private double UnitPrice; //to store Unit price
 //private double InventoryValue; //to store Inventory value
 //private double TotalInventory; //to store Total Inventory value

 public Inventory() //constructor with no initial variables

   {
	 this ("", 0.0, 0.0, 0.0);
   }

   public Inventory (String name, double number, double stock, double price)

	{
	  ItemName = name; //set Item name
	  ItemNumber = number; //set Item number
	  UnitsInStock = stock; //set Units in stock
	  UnitPrice = price; //set Unit price
	}

	 //method to set the Item name
	 public void setItemName( String name )

	 {
	   ItemName = name; //store the Item name
	 } //end method setItemName

	 //method to get the Item name
	 public String getItemName()

	 {
	   return ItemName;
	 } //end method getItemName

	 //method to set the Item number
	 public void setItemNumber( int number )

	 {
	   ItemNumber = number; //store the Item number
	 } //end method setItemNumber

	 //method to get the Item number
	 public double getItemNumber()

	 {
	   return ItemNumber;
	 } //end method getItemNumber

	 //method to set the UnitsInStock
	 public void setUnitsInStock( double stock )

	 {
	   UnitsInStock = stock; //store the Units in stock
	 } //end method setUnitsInStock

	 //method to get the UnitsInStock
	 public double getUnitsInStock()

	 {
	   return UnitsInStock;
	 } //end getUnitsInStock

	 //method to set the UntiPrice
	 public void setUnitPrice( double price )

	 {
	   UnitPrice = price; //store the Unit price
	 } //end method setUnitPrice

	 //method to get the UnitPrice
	 public double getUnitPrice()

	 {
	   return UnitPrice;
	 } //end getUnitPrice

	 //method to set Inventory value
	 //public void setInventoryValue(double value)

	 //{
	 //  InventoryValue = value;
	 //} //end method setInventoryValue

	 //method to get InventoryValue
	 //public double getInventoryValue()

	 //{
	 //  return InventoryValue;
	 //} //end method to getInventoryValue

	 public double getInventoryValue()

	 {
	   return UnitPrice*UnitsInStock;
	 } //end method to getInventoryValue

	 //method to set TotalInventory
	 public void setTotalInventory(double value)

	 {
	 TotalInventory = total;
	 } //end method setTotalInventory

	 //method to get TotalInventoty
	 public double getTotalInventory()

	 {
	 return TotalInventory;
	 } //end method to getTotalInventory


} //end class Inventory

//Inventory Test program part 1
//InventoryTest class

import java.util.Arrays;

public class InventoryTest1

{
	public static void main( String args[] )

	{

		// instantiate an Inventory object
		Inventory myInventory = new Inventory();

		//displays welcome message
		  System.out.println ("DVD Collection Inventory");
			System.out.println();//skips a line


		//create and initialize an array of DVDs
		int Inventory DVDs = new Inventory[5];

		DVDs[0] = new Inventory("The Day After Tomorrow", 112.1, 7, 12.99);
		DVDs[1] = new Inventory("Dejavu", 114.1, 25, 14.99);
		DVDs[2] = new Inventory("Live Free or Die Hard", 116.1, 15, 10.99);
		DVDs[3] = new Inventory("Fantastic Four", 102.1, 10, 0.99);
		DVDs[4] = new Inventory("The Island", 104.1, 5, 9.99);


		//For each array element, output value
								for (int count = 0; count < DVDs.length; count++ );
	   		System.out.printf("Product Name:  %s\n", DVDs[count].getItemName());
			System.out.printf("Item Number:  %4.2f\n", DVDs[count].getItemNumber());
			System.out.printf("Units In Stock:  %.2f\n", DVDs[count].getUnitsInStock());
			System.out.printf("Unit Price: $%4.2f\n", DVDs[count].getUnitPrice());
			System.out.printf("Inventory Value:  $%4.2f\n", DVDs[count].getInventoryValue());
			System.out.println(); //blank line to seperate products

			}//end main

}//end class InventoryTest1




Is This A Good Question/Topic? 0
  • +

Replies To: Java - Inventory Program Part 2

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java - Inventory Program Part 2

Posted 31 January 2010 - 08:26 PM

int Inventory DVDs = new Inventory[5];

Why do you have an int there? Remove that.

Oops, forgot to mention to change it to an array. Follow what sloth and Dogstopper say

This post has been edited by erik.price: 31 January 2010 - 08:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 Sloth  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 31-January 10

Re: Java - Inventory Program Part 2

Posted 31 January 2010 - 08:28 PM

change the line "int Inventory DVDs = new Inventory[5];"

into this:

Inventory[] DVDs = new Inventory[5];
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2873
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Java - Inventory Program Part 2

Posted 31 January 2010 - 08:31 PM

Since you are doing an array, you need to specify that it is an array.

Instead of this:
// You say it is an integer?
int Inventory DVDs = new Inventory[5];



make an array of Inventory
// Array see.
Inventory[] DVDs = new Inventory[5];



edit: sloth beat me to it. Nice Show! ;)

This post has been edited by Dogstopper: 31 January 2010 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 TracyLB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Re: Java - Inventory Program Part 2

Posted 31 January 2010 - 08:52 PM

View PostDogstopper, on 31 Jan, 2010 - 07:31 PM, said:

Since you are doing an array, you need to specify that it is an array.

Instead of this:
// You say it is an integer?
int Inventory DVDs = new Inventory[5];



make an array of Inventory
// Array see.
Inventory[] DVDs = new Inventory[5];



edit: sloth beat me to it. Nice Show! ;)


Thank you very much for the help with that...the code compiles and the program runs. However, it is still not displaying the entire array... What am I doing wrong?

It still won't display the entire array. What am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#6 TracyLB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Re: Java - Inventory Program Part 2

Posted 31 January 2010 - 09:06 PM

Here is the updated code which compiles and runs. However, it still will not go through the entire array. I've tried changing this part from count to 0 and it will only show the "0" line in the array. If I leave "count" in it won't work at all. Am I missing something in my for loop?

Here is my code


//Inventory Program Part 1
//Inventory Class - product class that holds the item number, the name of the product, the number of
//units in stock, and the price of each unit.


public class Inventory
{

 private double ItemNumber; //to store Item number
 private String ItemName; //to store Item name
 private double UnitsInStock; //to store number of units in stock
 private double UnitPrice; //to store Unit price
 //private double InventoryValue; //to store Inventory value
 //private double TotalInventory; //to store Total Inventory value

 public Inventory() //constructor with no initial variables

   {
	 this ("", 0.0, 0.0, 0.0);
   }

   public Inventory (String name, double number, double stock, double price)

	{
	  ItemName = name; //set Item name
	  ItemNumber = number; //set Item number
	  UnitsInStock = stock; //set Units in stock
	  UnitPrice = price; //set Unit price
	}

	 //method to set the Item name
	 public void setItemName( String name )

	 {
	   ItemName = name; //store the Item name
	 } //end method setItemName

	 //method to get the Item name
	 public String getItemName()

	 {
	   return ItemName;
	 } //end method getItemName

	 //method to set the Item number
	 public void setItemNumber( int number )

	 {
	   ItemNumber = number; //store the Item number
	 } //end method setItemNumber

	 //method to get the Item number
	 public double getItemNumber()

	 {
	   return ItemNumber;
	 } //end method getItemNumber

	 //method to set the UnitsInStock
	 public void setUnitsInStock( double stock )

	 {
	   UnitsInStock = stock; //store the Units in stock
	 } //end method setUnitsInStock

	 //method to get the UnitsInStock
	 public double getUnitsInStock()

	 {
	   return UnitsInStock;
	 } //end getUnitsInStock

	 //method to set the UntiPrice
	 public void setUnitPrice( double price )

	 {
	   UnitPrice = price; //store the Unit price
	 } //end method setUnitPrice

	 //method to get the UnitPrice
	 public double getUnitPrice()

	 {
	   return UnitPrice;
	 } //end getUnitPrice

	 //method to set Inventory value
	 //public void setInventoryValue(double value)

	 //{
	 //  InventoryValue = value;
	 //} //end method setInventoryValue

	 //method to get InventoryValue
	 //public double getInventoryValue()

	 //{
	 //  return InventoryValue;
	 //} //end method to getInventoryValue

	 public double getInventoryValue()

	 {
	   return UnitPrice*UnitsInStock;
	 } //end method to getInventoryValue

	 //method to set TotalInventory
	 //public void setTotalInventory(double value)

	 //{
	 //TotalInventory = total;
	 //} //end method setTotalInventory

	 //method to get TotalInventoty
	 //public double getTotalInventory()

	 //{
	 //return TotalInventory;
	 //} //end method to getTotalInventory


} //end class Inventory


//Inventory Test program part 1
//InventoryTest class

import java.util.Arrays;

public class InventoryTest1

{
	public static void main( String args[] )

	{

		// instantiate an Inventory object
		Inventory myInventory = new Inventory();

		//displays welcome message
		  System.out.println ("DVD Collection Inventory");
			System.out.println();//skips a line


		//create and initialize an array of DVDs
		Inventory[] DVDs = new Inventory[5];

		DVDs[0] = new Inventory("The Day After Tomorrow", 112.1, 7, 12.99);
		DVDs[1] = new Inventory("Dejavu", 114.1, 25, 14.99);
		DVDs[2] = new Inventory("Live Free or Die Hard", 116.1, 15, 10.99);
		DVDs[3] = new Inventory("Fantastic Four", 102.1, 10, 0.99);
		DVDs[4] = new Inventory("The Island", 104.1, 5, 9.99);


		//For each array element, output value
		for (int count = 0; count < DVDs.length; count++ );
		 DVDs[count] = count+1;

	   		System.out.printf("Product Name:  %s\n", DVDs[0].getItemName());
			System.out.printf("Item Number:  %4.2f\n", DVDs[0].getItemNumber());
			System.out.printf("Units In Stock:  %.2f\n", DVDs[0].getUnitsInStock());
			System.out.printf("Unit Price: $%4.2f\n", DVDs[0].getUnitPrice());
			System.out.printf("Inventory Value:  $%4.2f\n", DVDs[0].getInventoryValue());
			System.out.println(); //blank line to seperate products

	}//end main

}//end class InventoryTest1



Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,141
  • Joined: 27-December 08

Re: Java - Inventory Program Part 2

Posted 31 January 2010 - 09:10 PM

Your problem is that you have a semi-colon at the end of your loop for (int count = 0; count < DVDs.length; count++ );. You should instead have braces surrounding your code block. Like so:
for (int count = 0; count < DVDs.length; count++ ){ //notice the brace instead of the semi-colon
		 DVDs[count] = count+1;

			   System.out.printf("Product Name:  %s\n", DVDs[0].getItemName());
			System.out.printf("Item Number:  %4.2f\n", DVDs[0].getItemNumber());
			System.out.printf("Units In Stock:  %.2f\n", DVDs[0].getUnitsInStock());
			System.out.printf("Unit Price: $%4.2f\n", DVDs[0].getUnitPrice());
			System.out.printf("Inventory Value:  $%4.2f\n", DVDs[0].getInventoryValue());
			System.out.println(); //blank line to seperate products
}//notice the braces


Was This Post Helpful? 1
  • +
  • -

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java - Inventory Program Part 2

Posted 01 February 2010 - 01:41 PM

Also, unless I'm misreading something, these should all be DVDs[count] rather than DVDs[0]

System.out.printf("Product Name:  %s\n", DVDs[count].getItemName());
			System.out.printf("Item Number:  %4.2f\n", DVDs[count].getItemNumber());
			System.out.printf("Units In Stock:  %.2f\n", DVDs[count].getUnitsInStock());
			System.out.printf("Unit Price: $%4.2f\n",DVDs[count].getUnitPrice());
			System.out.printf("Inventory Value:  $%4.2f\n", DVDs[0].getInventoryValue());


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1