Claude Moore wrote:Method totalInventoryValue returns an array of inventory, not the sum over items. You assigned its retuned value to labelText variable, that's a string, so you get default object tostring() method output.
It was on this thread.A few minutes ago, Stephan van Hulst wrote:… accept that arrays and collections are not compatible, and to avoid accommodating the use of arrays.
Campbell Ritchie wrote:Use a Stream. Only works in Java8.
Start by creating a Stream; probably easiest with the static overloaded stream method of the Arrays class. That returns an IntStream and IntStream has a sum() method.
int i = Arrays.stream(myIntArray).sum()
Campbell Ritchie wrote:
It was on this thread.A few minutes ago, Stephan van Hulst wrote:… accept that arrays and collections are not compatible, and to avoid accommodating the use of arrays.
See if you can't change the array to a List<Integer>.
You would need the mapToInt() method to make sure the compiler knows you are dealing with Integers. The i -> i.intValue() bit is because you know you are dealing with Integers and you know they have an intValue() method. There are probably much neater ways to do that conversion.
Don't use doubles for money. Don't use floats for anything, unless some other code requires floats. Look at this old post and the two links therein for what you should use for money. Be sure to read the whole of the threads for both those links.Amie Mac wrote: . . . Since this is a cost value, wouldn't I need to be dealing with doubles or floats? . . .
Campbell Ritchie wrote:I trust we shan't see that sort of code in your GUI classes. That is application code which should be in different classes. You should have the application working before you try putting a GUI atop it. You must use the = operator because BigDecimal is immutable.
Campbell Ritchie wrote:If the value is in money, yes.
Read the old threads which show how BigDecimals work and a few things which can go wrong. Don't pass a double to a BigDecimal constructor.
Campbell Ritchie wrote:You are not returning the sum, but the array of inventory. You have shown the printout wrongly; there should be a [ at its beginning. You are supposed to return the BigDecimal sum and if you print it out you get a number. What you are doing is printing the array and arrays don't have an overridden toString method. So you get the same as with Object#toString.
I trust your values from the inventory are not in double format, because otherwise using BigDecimal you will simply immortalise the imprecision. Either change the inventory to use a precise form of number or round the values.
sum = sum.add(new BigDecimal(theInventory[i].inventoryValue()).round(...));
Why are you using a for loop rather than a for‑each loop?
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|