This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Writing a sum to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Writing a sum to a file" Watch "Writing a sum to a file" New topic
Author

Writing a sum to a file

Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
I'm trying to make a shopping list, i've succeded to create a file, print out every objects
name and price into my file. But the thing im working with now is to write the sum of the prices at the bottom of the shopping list.

How can i reach and add all of the prices of all objects to a string?

I've got these three clases:











Anyone got an idea of how to do it? I've tryed to make a method in the class Varor that returns a string that adds upp the prices, but it doesint seam to work.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Start with the simplest possible thing: how can you get the price of one object?

Next, write a loop which gets the price of each object and sums them up.
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
I would use something like
v.vara to get vara and v.pris to get the price. But the thing is that, then i will only get the name and price from the last object i added.
I need to get the name and the price (vara, pris) from each object in the list. Is there a way to get it, something like varor[i].pris and varor[i].vara ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Johannes Thorén wrote:I need to get the name and the price (vara, pris) from each object in the list. Is there a way to get it, something like varor[i].pris and varor[i].vara ?

Something like that, yes. But you don't have an array, so not exactly like that, no.

It seems to me you have a problem keeping concepts separate, and another problem combining concepts. You have an Iterator which gives you one Varor object after another. And you know how to get the price from a Varor object. So combine those two concepts and now you have a way to get the prices of all the Varor objects.

(By the way isn't "Varor" a plural word? If you look at the names of other Java classes you have used, are their names singular or plural? String -- singular -- it represents a string. File -- singular -- it represents a file. ArrayList -- singular -- it represents a kind of list. Varor -- plural -- what does it represent?)
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
It doesint work when i put every object from the ArrayList to an array eather.

I tryed two things:
first to make an integer, totalprice where i want to add everything, and another method to put every object into an array and get the price from there.



Your right, Varor is a plural word, means something like shopitems, since im adding several to a list.>
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Fixed the problem now. Made a method called returnPrice in class Varor that returns the price
and after i added added an object in my main class im adding the price to an int and then i got a total price when im done.

Thanks for the help tho
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing a sum to a file