Massive thunderstorm here right now, I am trying to get off before I lose power. I have an array that is accepting inventory input from the user. The value of each product is figured as it is entered, simple instock number muliplied by the product value.
I simply want to sum that value at the end of the array for a total inventory value.
Here is my code:

public class Compactdisk
{// begin class

	//InventoryCD class has 5 fields
	String Name; //  Name of cd
	float price; // price of cd
	int itemno; // item number of cd
	int nstock; // how many units in stock	
	int cdCount; // cd counter for array
	float value; // value for single cd inventory
	float totalValue; // value for entire inventory

	
	//Compact disk class constructor
	public Compactdisk()
	
		// 4 fields need to be set up
		{ 
		Name = "";
		price = 0;
		itemno = 0;
		nstock = 0;
		cdCount = 0;
		value = 0;
		totalValue = 0;
		}
		
		// set values
	   public void setName(String diskName)
	   {
	   Name = diskName;
	   }
		public void setPrice(float cdPrice)
	   {
	   price = cdPrice;
	   }
		public void setItemno(int cdItemno)
	   {
	   itemno = cdItemno;
	   }
		 public void setNstock(int cdStock)
	   {
	   nstock = cdStock;
	   }
		public void setValue(float cdValue)
		{
		value = cdValue;
		}
		public void setcdCount(int Count)
		{
		cdCount = Count;
		}
		public void setTotalValue(float invValue)
		{
		totalValue = invValue;
		}
		
	   // return values
		public String getName()
		{	
		return (Name);
		}
		public float getPrice()
		{	
		return (price);
		}
		public int getItemno()
		{	
		return (itemno);
		}
		public int getNstock()
		{	
		return (nstock);
		}
	
					
		// returns indivudual inventory value for a disk
	   public float getValue()
	   {
	   return(price * nstock);
	   }
		
		// returns total inventory value for all disks
		public float getTotalValue()
		{
		return (totalValue += cds[cdCount].getValue());
		}
			
			
		
			
}// end class

I am getting these two errors back:
Compactdisk.java:86: cannot find symbol
symbol : variable cds
location: class Compactdisk
return (totalValue += cds[cdCount].getValue());
^
Compactdisk.java:86: inconvertible types
found : <nulltype>
required: float
return (totalValue += cds[cdCount].getValue());
^
2 errors


I thought this would be simple, but I have played with these parameters and formulas for 10 to 12 hours now with no progress? I think I have it close, but just do not see what I am doing wrong.
Am I just way off base and not skilled enough to know it?
Any help would be appreciated.

Member Avatar for iamthwee

What are you trying to do your code makes little sense?

The first error is saying that the CD Class doesn't know about the other class. This line isn't really necessary anyway.

What i would do would make a float variable in the inventory class and every time you loop and insert another element into the array add it to the float: e.g.

float runningTotal = 0;

runningTotal = runningTotal + arrayelement[0]

Sorry iamthwee, here is my other class, as it now stands. Maybe my question will make more sense now. I am not very good yet, so maybe it will make less sense. ;)

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

public class Inventory
{// begin class Inventory
	public static void main(String[] args)	
	{//begin method main
	
	
	// create cd Array
	Compactdisk[] cds = new Compactdisk[5];

	cds[0] = new Compactdisk(); // adds to array
	cds[0].getName();
	cds[0].getPrice();
	cds[0].getItemno();
	cds[0].getNstock();
	
	int cdCount = 0;
	float totalValue = 0;
	
	Scanner input = new Scanner(System.in);  // create scanner
	
	
	
			// begin display method
			System.out.print("Enter up to 5 CD Names or STOP to Exit: ");
			String nameInput = input.next(); //read cd name
						
			
		  	while (!nameInput.equalsIgnoreCase("STOP"))
			{// begin main While
			
				cds[cdCount] = new Compactdisk();
				cds[cdCount].setName(nameInput);

				System.out.print("Enter Price of this CD: "); // prompt for price
				cds[cdCount].setPrice(input.nextFloat());	// price input from user.
				while (cds[cdCount].getPrice()<= 0)
				{// begin while
				System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
				cds[cdCount].setPrice(input.nextFloat()); // cd price loop from user.
				} // End while
		
				System.out.print("Enter CD Item Number: "); // prompt for cd item number
				cds[cdCount].setItemno(input.nextInt()); // cds item number input from user
		
				System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
				cds[cdCount].setNstock(input.nextInt()); // cds in stock input from user
				
				        
				System.out.print("CD "+cds[cdCount].getName()+", Item Number "+cds[cdCount].getItemno()+","); // display name
				System.out.printf(" is worth %c%.2f.\n", '$', cds[cdCount].getPrice()); // display individual price
				System.out.printf("We have %d copies in stock, making our inventory for this cd worth %c%.2f\n", cds[cdCount].getNstock(), '$', cds[cdCount].getValue()); //inventory value
		
				cdCount++;
				totalValue = totalValue + cds[cdCount].getValue();
				
				
				System.out.print("Enter up to 5 CD Names or STOP to Exit: "); // internal loop prompt
				nameInput = input.next(); //name input from user
																
			} // End main While
			
			
		System.out.printf("Entire Inventory for all CDs is Worth %c%.2f.\n", totalValue);
		System.out.print("Ending Program.");
	

	}// end method main
} // end class Payroll

Cerberus, I almost slapped myself that was so simple. I modified it (as you see above) and put it in, compiled it, everything seems great ... but when you exit the while loop every goes screwy. I am getting a "main" java.lang.nullpointerexception error now.
This is new to me, actually having it compile, and error when executing.

Thought I figured it out, but I just made it worse. I moved the count line to run after the totalValue line. Logic being, the count was moving forward before I was ready for it to.
Thought I had it because now I can run the loop again. When I enter STOP to leave the loop, and hopefully see my total value I get a whole string of errors. Again, it complies, but errors on execution.
Errors are:
Illeagal format conversion, Format specifier and that group.

No real idea what that means. I think it might have something to do with the count being integer and the total being float? Maybe it does not like the way I am trying to print it?

Your almost there you just need to handle the exception. Try this:

try
{
       totalValue = totalValue + cds[cdCount].getValue();    
}
catch(NullPointerException npe)
{
        //handle exception
}

I have never seen that code before, what is it?
I tried it just to see if I could see what it does, but nothing changes.
Everything compliles, error on execution, just when I leave the loop.
Looks like it starts to execute, but just does not like my formating of the %c%.2f./n, or the format of the variable I am trying to put there.

Exact message reads: Entire Inventory for all CDs is Worth Exceptio in thread "main" java.util.IllegalFormatConversionException: c != java.lang.Float
at java.util.Formatter$FormatSpecifier.failConversion (Formater.java:3978)
at java.util.Formatter$FormatSpecifier.printCharacter
(Formatter.java:2006)


This is way over my head.

Thanks for your continued help. I know there are lots better things you could be using your Sunday for. Here is my code as it stands with your "try/catch" solution implimented.

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

public class Inventory
{// begin class Inventory
	public static void main(String[] args)	
	{//begin method main
	
	
	// create cd Array
	Compactdisk[] cds = new Compactdisk[5];

	cds[0] = new Compactdisk(); // adds to array
	cds[0].getName();
	cds[0].getPrice();
	cds[0].getItemno();
	cds[0].getNstock();
	
	int cdCount = 0;
	float totalValue = 0;
	
	Scanner input = new Scanner(System.in);  // create scanner
	
	
	
			// begin display method
			System.out.print("Enter up to 5 CD Names or STOP to Exit: ");
			String nameInput = input.next(); //read cd name
						
			
		  	while (!nameInput.equalsIgnoreCase("STOP"))
			{// begin main While
			
				cds[cdCount] = new Compactdisk();
				cds[cdCount].setName(nameInput);

				System.out.print("Enter Price of this CD: "); // prompt for price
				cds[cdCount].setPrice(input.nextFloat());	// price input from user.
				while (cds[cdCount].getPrice()<= 0)
				{// begin while
				System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
				cds[cdCount].setPrice(input.nextFloat()); // cd price loop from user.
				} // End while
		
				System.out.print("Enter CD Item Number: "); // prompt for cd item number
				cds[cdCount].setItemno(input.nextInt()); // cds item number input from user
		
				System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
				cds[cdCount].setNstock(input.nextInt()); // cds in stock input from user
				
				        
				System.out.print("CD "+cds[cdCount].getName()+", Item Number "+cds[cdCount].getItemno()+","); // display name
				System.out.printf(" is worth %c%.2f.\n", '$', cds[cdCount].getPrice()); // display individual price
				System.out.printf("We have %d copies in stock, making our inventory for this cd worth %c%.2f\n", cds[cdCount].getNstock(), '$', cds[cdCount].getValue()); //inventory value
				
				try
				{
				totalValue = totalValue + cds[cdCount].getValue();
				}
				
				catch(NullPointerException npe)
				{
				
				} // handles exception
				
				cdCount++;
				
				System.out.print("Enter up to 5 CD Names or STOP to Exit: "); // internal loop prompt
				nameInput = input.next(); //name input from user
																
			} // End main While
			
			
		System.out.printf("Entire Inventory for all CDs is Worth %c%.2f.\n", totalValue);
		System.out.print("Ending Program.");
	

	}// end method main
} // end class Payroll

The worst part of this is that this was going to be a quick easy thing for me. I want to sort everything by name after this, and that was the hard part. I have been reading and trying stuff for that in my down time on this issue, and it is worse than I thought! :S

Problem solved. Simple missing '$' in the print command.
Should have been.
System.out.printf("Entire Inventory for all CDs is Worth %c%.2f.\n", '$', totalValue)

How ya like that? Thanks everyone for their help.

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.