• Post Reply Bookmark Topic Watch Topic
  • New Topic

summing values with the same key  RSS feed

 
Jeremiah Hoyle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been working through a beginner java class and this is my last task and I am stuck. I need to sum the values associated with a specific ID in a map, and a map just shows the last values entered with that key.

Here is exactly what I am doing, I am reading a text file looping through the file adding the data to variables and adding the key and value to the map. This is an example of what I am readin
500000.00 101
100000.00 101
1000000.00 107

the first number is the value and the second is the key. I need it to output as
101 600000.00
107 1000000.00

here is my while loop


I was think some sort of "if" statement that uses containskey might work, so far all my attempts at that have failed. But then I have been grasping in the dark on how to code that even. Any help would be appreciated.

 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the put method replaces an existing value in a map with a new value.

So you have two options as I see it.
1. Keep the sum in the map as the value. This involves grabbing the current value for a key and summing it with your new value. Disadvantage is you lose the knowledge of the values that went into that sum.

2. Change the parameters of the map. I imagine your map is probably declared as a Map<Integer, Double>. If you change this to a Map<Integer, List<Double>>, then each key points to the List of values associated with that ID. You can work with the List of values to get your sum. Disadvantage here is it's a little more complicated to read and implement.

I prefer the second option because it's more flexible. For example if someone decided they wanted a list of items they bought along with the sum, I could do it. Can't do that with the first method.
For a beginner Java class the first would probably be sufficient.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

There is a code tag button that allows you pretty-format your code.

A few code style comments as well:
I find the one or two letter prefix in front of variables confusing. I thought stPrice was a String until you were doing addition with it.
You may want to add validation in case your file isn't formatted exactly the way you expect.

It's confusing that your sample input has two fields but you are reading four using in.next()?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This bit of code is a bit dodgy as well.

Only the first line is guaranteed to work. The next three could all fail if there is nothing else on the input stream. you should call in.hasNext before each one.

Edit: which I just noticed Tina had already mentioned.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at the Double.parseDouble and pType.toUpperCase lines.
Good catch
 
Jeremiah Hoyle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks guys for the tips, the file I am reading has four columns but they don't go into the map and therefore didn't need to be addressed so I did not include them in the example. Sorry for the confusion on that part. Also yeah I could use some validation in case of different formatting, but looking at the rubric for my task assignment, they just want it to work with this one file they give me so I was not going to worry about that.
 
Jeremiah Hoyle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the multiple post, but you said I shouldn't use the one or two letter prefix was confusing. I had not thought about that, but it makes sense. I was just doing it the way I was taught for this class. Should I just always use full words? such as stPrice was short for subtotal price...at least in my mind. I went with option 1, I briefly considered option 2 but decided to just get this done as option 1 will work. here is the code I used

Thanks for all the help
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!