I realize this code has been seen a million times already. I am trying to 'start' on part 3 of this. I do not wish for it to be done for me just a starting point for this subclass. These are the directions:

Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name 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.
This is what I have to start with:

import java.util.Scanner;

//Inventory program
//This program monitors DVD inventory, price, and stock
public class Inventory 
{//new class inventory
	
	public static void main (String args[])
	{//begin main method
		
		System.out.print("Welcome to the DVD inventory program");//welcome	
		System.out.println();//blank line
		System.out.println("This program monitors DVD inventory, price, and stock.");//program purpose
		System.out.println();//blank line
		
		
		//declare variables
		String DvdTitle;
        double ItemNumber = 0.00;
        double DvdPrice = 0.00;
        int NumberInStock = 0; 
        int count;//counter control variable
			
	    Scanner input = new Scanner (System.in);
			
		System.out.println("How many DVDs would you like to enter?");//prompt for length of array
		count = input.nextInt();//accept user input
			
		//declare arrays
		String[] DvdTitleArray = new String[count];
		double[] ItemNumberArray = new double[count];
		double[] DvdPriceArray = new double[count];
		int[] NumberInStockArray = new int[count];
		double[] StockValueArray = new double[count];
			
		for (int i = 0; i<count; i++)
		{
		    System.out.println();//blank line
            System.out.println("Enter title of DVD. ");//prompt for title
            DvdTitle = input.next();//accept user input
            DvdTitleArray[i] = DvdTitle;
            
		    System.out.println();//blank line
		    System.out.println("Please enter item Number.");//request item number
		    ItemNumber = input.nextDouble();//user input item number as double
		    ItemNumberArray[i] = ItemNumber;    
			
	    	System.out.println();//blank line
	    	System.out.println("Please enter price of DVD.");//request price
	    	DvdPrice = input.nextDouble();//accept user input
	    	DvdPriceArray[i] = DvdPrice;
	    	    
	    	if (DvdPrice <=0)
	    	{//if statement for if price is equal or less than "0"
		    	System.out.println("Invalid price entry");
		    	System.out.println();//blank line
		    	System.out.println("Please reenter valid price.");
		    	DvdPrice = input.nextDouble();//accept user input
		    	DvdPriceArray[i] = DvdPrice;
	    	}//end if
			   
	    	else
	    	    	
	    	    System.out.println();//blank line
	    	    System.out.println("Please enter number of DVDs in stock.");//request number of DVDs in staock
		    	NumberInStock = input.nextInt();//accept user input
		    	NumberInStockArray[i] = NumberInStock;	
		    	
		    	StockValueArray[i] = (DvdPrice * NumberInStock);
			}//end for
		
		double totalInventory = computeValue(StockValueArray);
			
		for (int i = 0; i<count; i++)
		{//print information from array
			System.out.println("DVD Title:" + DvdTitleArray[i]);
			System.out.println("Item Number:" + ItemNumberArray[i]);
			System.out.println("Price:" + DvdPriceArray[i]);
			System.out.println("Number in Stock:" + NumberInStockArray[i]);
			System.out.println("Stock Value:" + StockValueArray[i]);
		    System.out.println();//blank line
		}//end for 
		
		System.out.println("Total Inentory:" +totalInventory);
			
	}//end main method	

	private static double computeValue (double[] StockValueArray)
	{
		double StockValue = 0.00;
		
		for (int i = 0; i<StockValueArray.length; i++)
		{
			StockValue += StockValueArray[i];
		}
		return StockValue;
	}//end method
	
}//end inventory class

First of all it looks like you have a bunch of arrays that contain related information. . the item number, price of DVD, and name of a DVD all go together, correct? If the answer is yes, then you should create a class, perhaps called DVD, that has the necessary fields to encapsulate this information. For example:

public class DVD{
String title = null;
double itemNumber = 0.0; 
double dvdPrice = 0.0;
double stockValue = 0.0;
}

You'd need to add a constructor so that you could create a new DVD. This would transform the above class into the following:

public class DVD{
String title = null;
double itemNumber = 0.0; 
double dvdPrice = 0.0;
double stockValue = 0.0;

public DVD(String theTitle, double item, double price, double value){
title=theTitle;
itemNumber=item;
dvdPrice=price;
stockValue=value;
}
}

Additionally, you would probably need getters and setters for that class. But by using that class, you would avoid the need to have four different arrays when the information is all related - since it is related, it should be stored in the same Object. This would mean in your main class, you would have the following array:

DVD[] myDVDs = new DVD[count];

Then, when you asked the user what the prices were and whatnot, you could use the constructor of your DVD class to create a new DVD. And you would then put that DVD into your array. Looking at the methods that your teacher says you need to write, you could accomplish your "calculate inventory value" method quite easily. Since your array would be an array of DVDs, you would simply write a for loop that goes through the DVDs, and you would add the value of each DVD to a variable.

Hope that helps.

Ok I think I have a good start going here, though I do have a few more questions;
1) (Inventory class) @line 47 I should be using "input.nextLine();" to be able to enter more than one word in the title, but when I do it will stop with an error saying "Exception in thread 'main' java.util.inputMismatchException"
2) (inventory class) @line 39 As you suggested I used "DVD[] myDVD = new DVD[count]" and says local variable is never read. I have the same problem @line 25 with "restockFee". Is this a problem with them not being initialized or am I not calling the method in my sub-class?

import java.util.Scanner;

//Inventory program
//
//This program monitors DVD inventory, price, and stock
public class Inventory 
{//new class inventory
	
	public static void main (String args[])
	{//begin main method
		
		System.out.print("Welcome to the DVD inventory program");//welcome	
		System.out.println();//blank line
		System.out.println("This program monitors DVD inventory, price, and stock.");//program purpose
		System.out.println();//blank line
		
		
		//declare variables
		String DvdTitle;
        double ItemNumber = 0.00;
        double DvdPrice = 0.00;
        int NumberInStock = 0; 
        int count;//counter control variable
        double restockFee = 0.00;
			
	    Scanner input = new Scanner (System.in);
			
		System.out.println("How many DVDs would you like to enter?");//prompt for length of array
		count = input.nextInt();//accept user input
			
		//declare arrays
		String[] DvdTitleArray = new String[count];
		double[] ItemNumberArray = new double[count];
		double[] DvdPriceArray = new double[count];
		int[] NumberInStockArray = new int[count];
		double[] StockValueArray = new double[count];
		
		DVD[] myDVD = new DVD[count];
			
		for (int i = 0; i<count; i++)
		{
		    System.out.println();//blank line
            System.out.println("Enter title of DVD. ");//prompt for title
            DvdTitle = input.next();//accept user input
            DvdTitleArray[i] = DvdTitle;
            
		    System.out.println();//blank line
		    System.out.println("Please enter item Number.");//request item number
		    ItemNumber = input.nextDouble();//user input item number as double
		    ItemNumberArray[i] = ItemNumber;    
			
	    	System.out.println();//blank line
	    	System.out.println("Please enter price of DVD.");//request price
	    	DvdPrice = input.nextDouble();//accept user input
	    	DvdPriceArray[i] = DvdPrice;
	    	    
	    	if (DvdPrice <=0)
	    	{//if statement for if price is equal or less than "0"
		    	System.out.println("Invalid price entry");
		    	System.out.println();//blank line
		    	System.out.println("Please reenter valid price.");
		    	DvdPrice = input.nextDouble();//accept user input
		    	DvdPriceArray[i] = DvdPrice;
	    	}//end if
			   
	    	else
	    	    	
	    	    System.out.println();//blank line
	    	    System.out.println("Please enter number of DVDs in stock.");//request number of DVDs in staock
		    	NumberInStock = input.nextInt();//accept user input
		    	NumberInStockArray[i] = NumberInStock;	
		    	
		    	StockValueArray[i] = (DvdPrice * NumberInStock);
			}//end for
		
		double totalInventory = computeValue(StockValueArray);
			
		for (int i = 0; i<count; i++)
		{//print information from array
			System.out.println("DVD Title:" + DvdTitleArray[i]);
			System.out.println("Item Number:" + ItemNumberArray[i]);
			System.out.println("Price:" + DvdPriceArray[i]);
			System.out.println("Number in Stock:" + NumberInStockArray[i]);
			System.out.println("Stock Value:" + StockValueArray[i]);
		    System.out.println();//blank line
		}//end for 
		
		System.out.println("Total Inventory:" +totalInventory);
			
	}//end main method	

	private static double computeValue (double[] StockValueArray)
	{
		double StockValue = 0.00;
		
		for (int i = 0; i<StockValueArray.length; i++)
		{
			StockValue += StockValueArray[i];
		}
		return StockValue;
	}//end method
	
}//end inventory class
public class DVD 
{
	//declare variables
	String DvdTitle = "";
	double ItemNumber = 0.00;
	double DvdPrice = 0.00;
	int NumberInStock = 0;
	double restockFee = 0.00;
	
	//constructor
	public DVD (String DvdTitle, double ItemNumber, double DvdPrice, int NumberInStock, double RestockFee)
	{
		this.DvdTitle = DvdTitle;
		this.ItemNumber = ItemNumber;
		this.DvdPrice = DvdPrice;
		this.NumberInStock = NumberInStock;
		this.restockFee = RestockFee;
	}
	
	//get and set methods
	public String getDvdTitle(){
		return DvdTitle;
	}
	
	public void setDvdTitle(String DvdTitle){
		this.DvdTitle = DvdTitle;
	}
	
	public double getItemNumber(){
		return ItemNumber;
	}
	
	public void setItemNumber(double ItemNumber){
		this.ItemNumber = ItemNumber;
	}
	
	public double getDvdPrice(){
		return DvdPrice;
	}
	
	public void setDvdPrice(double DvdPrice){
		this.DvdPrice = DvdPrice;
	}
	
	public int getNumberInStock(){
		return NumberInStock;
	}
	
	public void setNumberInStock(int NumberInStock){
		this.NumberInStock = NumberInStock;
	}
	
	public double getRestockFee(){
		return (NumberInStock * DvdPrice * .05);
	}

}

Ok I think I have a good start going here, though I do have a few more questions;
1) (Inventory class) @line 47 I should be using "input.nextLine();" to be able to enter more than one word in the title, but when I do it will stop with an error saying "Exception in thread 'main' java.util.inputMismatchException"
2) (inventory class) @line 39 As you suggested I used "DVD[] myDVD = new DVD[count]" and says local variable is never read. I have the same problem @line 25 with "restockFee". Is this a problem with them not being initialized or am I not calling the method in my sub-class?

1) Your program says, "how many DVDs would you like to enter?". Then, you get the response from the user and you read it in as an int. However, what the user entered was not just an int. Let's say the user enters 10 and then presses enter. Their input was 10 and a newline ( \n ). However, when you said input.nextInt(), you only read in the 10, and you left the newline there. So change your code to the following:

System.out.println("How many DVDs would you like to enter?");//prompt for length of array
		count = input.nextInt();//accept user input
		String getRidOfThisNewline = input.nextLine();

You have to think about "how to read in data" as what the user entered, not in terms of what you want to get. Thinking about it as "I want to read in an int" is what you want to get -- not what was entered. Since the \n was entered, then remained there (because nextInt did not read it in), the next time you attempted to read in a value as a double, it couldn't succeed. Also, be aware that methods like nextInt and nextDouble might not do what you think -- you probably wanted it to go through the input, and return the next integer to you. What it really does is it finds the next bit of input and attempts to read it in as an integer - it does not 'skip' past other input that doesn't match the pattern. For example, if your user entered "blah blah 44", it would fail, since blah is read in by nextInt, but it is not an integer. Javadocs are extremely helpful with stuff like this - I don't always read or understand them perfectly either - but overall they help a lot.

2) The message "local variable is never read" means that you declared a variable (which is what the line of code DVD[] myDVD = new DVD[count] does), however, you never used that variable anywhere. The point of making that whole DVD class and making an array of DVDs was so that you would not need all of these arrays:

String[] DvdTitleArray = new String[count];
		double[] ItemNumberArray = new double[count];
		double[] DvdPriceArray = new double[count];
		int[] NumberInStockArray = new int[count];
		double[] StockValueArray = new double[count];

With an array of DVD's, you no longer need any of those other arrays, since one DVD contains one DVD title, one item number, one DVD price, one number in stock, and one stock value. So when you have an array of 10 DVD's, you have everything that the user entered stored into those DVD's. I'll help you get started by editing your program very slightly, but only enough so that you can understand enough to do the rest yourself. Here are your classes after I made my changes, I put comments above my changes.

(Be back to edit them in shortly):

System.out.println("How many DVDs would you like to enter?");//prompt for length of array
		count = input.nextInt();//accept user input

                //GET RID OF THAT NEWLINE
		String getRidOfThisNewline = input.nextLine();
		//declare arrays
		DVD[] myDVD = new DVD[count];
			
		for (int i = 0; i<count; i++)
		{
			//We want 'count' number of new DVD's. 
		    System.out.println();//blank line
            System.out.println("Enter title of DVD. ");//prompt for title
            DvdTitle = input.nextLine();//accept user input
            
		    System.out.println();//blank line
		    System.out.println("Please enter item Number.");//request item number
		    ItemNumber = input.nextDouble();//user input item number as double
			
	    	System.out.println();//blank line
	    	System.out.println("Please enter price of DVD.");//request price
	    	DvdPrice = input.nextDouble();//accept user input
	    	
	    	//Create a new DVD. Note: I obviously didn't use your constructor properly.
	    	//You'll have to edit it so the things are passed in correctly.
	    	DVD newDVD = new DVD(DvdTitle, ItemNumber, DvdPrice, OtherStuff);
	    	
	    	//Put the dvd you just created into the array of DVDs.
	    	myDVD[count] = newDVD;
		}

Sorry, that post was very long. However there are a few other comments I have:

1) The for loop that I made some changes to is obviously incomplete. I intentionally erased some of your working code. The idea is to show you how to create a DVD and put it in the array - I'm not saying that your other code doesn't work.

2) In the for loop that you wrote, I noticed that you have an "if something is < 0" statement, followed by an "else" statement. The else statement doesn't have any brackets around it. . I'm not sure if that was a mistake or not. It's line 66 in post 3 (your second post you made in this thread).

This article has been dead for over six months. Start a new discussion instead.