I'm making an inventory program for class. It has a loop that should cycle through the array and print the information. Part of the loop is supposed to add the value from each inventory item together to get the total value of the inventory. However, instead the program simply adds the last value entered to itself repeatedly. There is no compiler error, but the program isn't doing what it should.
Here is my code for the Product class.
And my loop -
for ( int counter = 0; counter < inventory.length; counter++ )
-
{
-
System.out.print( "What is the product number of the CD? ");
-
String theNumber = input.next();
-
CD.setNumber( theNumber );
-
System.out.println();
-
-
System.out.print( "What is the name of the Artist? ");
-
String theArtist = input.next();
-
CD.setArtist( theArtist );
-
System.out.println();
-
-
System.out.print( "How many of this CD are in stock? ");
-
int theUnits = input.nextInt();
-
CD.setUnits( theUnits );
-
System.out.println();
-
-
System.out.print( "How much does this CD cost? ");
-
double thePrice = input.nextDouble();
-
CD.setPrice( thePrice );
-
System.out.println();
-
-
CD.value( CD.getPrice(), CD.getUnits() );
-
-
inventory[ counter ] = new Product( theNumber, theArtist, theUnits, thePrice, CD.value( CD.getPrice(), CD.getUnits() ) );
-
}
-
-
for ( int counter = 0; counter < inventory.length; counter++ )
-
{
-
total += CD.value( CD.getPrice(), CD.getUnits() );
-
System.out.println( inventory[counter ] );
-
}
-
-
System.out.printf( "The total value of the inventory is %.2f\n\n", total );
-
Got the answer.
total += inventory[counter].value( CD.getPrice(), CD.getUnits());
This properly adds the value through the array!
12 2240
What is this CD variable? You only have one CD variable so how do you expect to get a sum of all the records when you only store one at any time?
Move the total into the prior loop and it should total fine. But you will still only be storing one CD at a time.
CD is an object made from Product class. Inventory[] is filled with CD's. I tried moving the total calculation to the other loop, but it didn't work.
If you were trying to add data in array together, how would you go about it. Perhaps I'm tackling the problem the wrong way.
The method is fine. It's just that you're using the single CD to get a total of an array. You haven't linked it to the inventory array in any way.
You should either total each item in the inventory array or set the CD to a single item in the inventory array before getting the value.
Post the code that you used when you moved the calculation to the other loop, that should have worked.
This is the changed code. -
//Using inventory.length as a counter, loop input and output
-
for ( int counter = 0; counter < inventory.length; counter++ )
-
{
-
System.out.print( "What is the product number of the CD? ");
-
String theNumber = input.next();
-
CD.setNumber( theNumber );
-
System.out.println();
-
-
System.out.print( "What is the name of the Artist? ");
-
String theArtist = input.next();
-
CD.setArtist( theArtist );
-
System.out.println();
-
-
System.out.print( "How many of this CD are in stock? ");
-
int theUnits = input.nextInt();
-
CD.setUnits( theUnits );
-
System.out.println();
-
-
System.out.print( "How much does this CD cost? ");
-
double thePrice = input.nextDouble();
-
CD.setPrice( thePrice );
-
System.out.println();
-
-
CD.value( CD.getPrice(), CD.getUnits() );
-
-
inventory[ counter ] = new Product( theNumber, theArtist,
-
-
theUnits, thePrice, CD.value(
-
CD.getPrice(), CD.getUnits() ) );
-
-
total =+ CD.value( CD.getPrice(), CD.getUnits() );
-
}
-
-
for ( int counter = 0; counter < inventory.length; counter++ )
-
{
-
-
System.out.println( inventory[counter ] );
-
}
-
-
System.out.printf( "The total value of the inventory is %.2f\n\n",
-
-
total );
-
Shouldn't that be += and not =+?
It wasn't working either way, but I figured try each. I've exhausted anything I thought was the problem. I'm just trying anything at this point. Desperation...
Have you tried outputting each of those inputs to make sure they're getting in there correctly?
When I run the code, I get an output that shows the product#, name, price, amount, and value for each product in the array. The value show up and are correct. But when I try to add the values together (using the various versions of code I tried) either I get 0.00, or the only the last value calculated, or the last value times the array.length.
I don't see anything in your code that actually outputs the values. Are you sure you're not just seeing the inputs?
-
for ( int counter = 0; counter < inventory.length; counter++ )
-
{
-
-
System.out.println( inventory[counter ] );
-
}
-
-
-
After the user inputs the data which is added to the array, this code shows on the screen a summation of the data. The method that controls how that data is displayed in found in the Product class.
Got the answer.
total += inventory[counter].value( CD.getPrice(), CD.getUnits());
This properly adds the value through the array!
Glad you got it working. You should mark your post as the answer.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ercwebdev |
last post by:
Can anyone tell me why this For loop isn't working (see below)
The error message is says:
Error: 'document.emailForm.FNAME_CHILD' is null or not an object
Thanks in advance,
Alan R
|
by: Drew |
last post by:
I am building an application for keeping track of user permissions here at
work. I have built the interfaces, and am now working on the processing
page for inserting to the database. I am having...
|
by: Matt |
last post by:
I am in the process of creating a custom .NET object (this is my first one).
I have created a few classes and some of them are collections. When I try to
iterate through a collection I receive the...
|
by: upernikaw |
last post by:
Hello,
I am attempting to create a nested loop (in Access 2003/VB) that will print a report for a set of user defined months inputed on a form and that will print out for every Client. So the first...
|
by: jimmy |
last post by:
I am working on a project which tracks 'bad' words in IE and im using a
For loop to check for an array of words in he address bar. I have
included the broken code below. Any pointers on why it isnt...
|
by: tamarindm |
last post by:
I need to play a .wav file over and over again. I am using the
following code.
private static extern bool PlaySound( string lpszName, int hModule, int
dwFlags );
public int SND_ASYNC =...
|
by: Dipti Singh |
last post by:
Hi,
I have two asp pages. when both contain lots of coding, loops etc.
if i run the first page that contain for loop for creating array of textboxes,
then this page run successfully.
but if...
|
by: Prosdo |
last post by:
Hey Everyone. I have a program for school that is suppose to project how many seconds it takes a projectile to hit the ground. I have the formula, but the loop is not posting to my output file. Can...
|
by: pdring |
last post by:
Hi everyone,
I have had a look through the site but can't find anything to help with a little problem i am experiencing with a college assignment. I wonder if anyone would like to help out?
My...
|
by: dwcolt |
last post by:
Help! I'm hoping that someone can point me to a VB script that will do a fairly simple task.
I have a Microsoft Access table that lists the full directory path for a number of files.
I am...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |