Results 1 to 7 of 7
Thread: HashMap problem
- 03-15-2012, 07:40 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 13
- Rep Power
- 0
HashMap problem
Hello all, after much dissapointment from a previous programming forum I hope I can get an answer from this one :) I'll try to be a specific as possible.
So I have some students which in this case will will be the Key and their mark which will be their Value.
This is my code so far.
The problem I'm having is this runs through the list and ignores duplicates, which it's meant to do but it still needs to count the mark just discard the name.Java Code:import java.util.*; import java.io.IOException; public class StudentMarks { public static void main(String[] args) throws IOException { Map<String, Integer> mp = new HashMap<String, Integer>(); mp.put("Simon", 4); mp.put("Anna", 10); mp.put("Simon", 4); mp.put("Anna", 9); mp.put("Anna", 5); mp.put("Edward", 10); Iterator<String> i= mp.keySet().iterator(); String k; while(i.hasNext()){ k=i.next(); System.out.println(k+"\t"+mp.get(k)); } } }
The output needs to be as follows
Merit order
Edward 1 10.0
Anna 3 8.0
Simon 2 4.0
So as you can see Edward as 1 mark with average of 10
Anna has 3 marks with average of 8
Simon has 2 marks with average of 4.
I just can't think what I need to change in that loop to make this happen. Any ideas? Would be much appreciated.
Please try not to type just one line if you could edit my actual code and type in bold what you have changed that would help me understand the code a lot more. Thanks again
- 03-15-2012, 07:46 PM #2
Re: HashMap problem
Since this is a crospost, it's considered polite to include a link to that other post. We don't want to waste our time, or yours, repeating advice you've already received.
So what do you want the map to hold? All of the grades? The average? The count? Both? Something else?
That's not really how this works. This isn't a code service. We'll try to work through your problem with you, but don't count on somebody handing you code. You wouldn't really learn that way anyway.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-15-2012, 07:50 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 13
- Rep Power
- 0
Re: HashMap problem
That's just it I haven't received any help so I haven't had any advice for it to be repeated lol. I just want it to print it out in Merit order, so number of scores then next to it the average.
As you see with the Merit ordering below
Merit order
1 Edward 1 10.0 (Edward had 1 score and average of that score was 10)
2 Anna 3 8.0 (Anna had 3 scores and her average was 8)
3 Simon 2 4.0 (Simon had 2 scores and his average was 4)
- 03-15-2012, 08:00 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: HashMap problem
Keeping all the individual scores for each person is enough to find the average and the number of scores; don't use a Map<String, Integer> but use a Map<String, List<Integer>> instead.
b.t.w. this is not so advanced ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-15-2012, 08:02 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 13
- Rep Power
- 0
- 03-15-2012, 08:13 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 03-16-2012, 06:03 PM #7
Member
- Join Date
- Mar 2012
- Location
- Novosibirsk, Russia
- Posts
- 13
- Rep Power
- 0
Re: HashMap problem
So for each student, you have to keep 2 values: mark count and mark sum (or average). So you need to create a class StudentInfo {count; sum;}
and HashMap<String, StudentInfo>, and the method
Java Code:put(String name, int grade) { info = new StudentInfo() if was null; info.count++; info.sum+=grade; }
Similar Threads
-
HashMap problem
By vince8864 in forum New To JavaReplies: 1Last Post: 02-13-2012, 11:51 AM -
hashmap or search problem
By flushdabuffer in forum AndroidReplies: 0Last Post: 04-09-2011, 01:55 PM -
hashmap problem
By minotaurus in forum Advanced JavaReplies: 5Last Post: 03-16-2011, 11:24 AM -
Problem with HashMap
By maz09 in forum New To JavaReplies: 2Last Post: 04-14-2010, 09:40 PM -
Iterating through HashMap problem
By JordashTalon in forum New To JavaReplies: 1Last Post: 01-28-2009, 11:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks