Results 1 to 7 of 7

Thread: HashMap problem

  1. #1
    Java Junior is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default 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.

    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 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.

    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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: HashMap problem

    Quote Originally Posted by Java Junior View Post
    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.
    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.

    Quote Originally Posted by Java Junior View Post
    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.
    So what do you want the map to hold? All of the grades? The average? The count? Both? Something else?


    Quote Originally Posted by Java Junior View Post
    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
    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!

  3. #3
    Java Junior is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default 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)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Java Junior is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: HashMap problem

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    So if I just change that there's nothing more I need to change within the code? I'll try that in a bit thanks

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: HashMap problem

    Quote Originally Posted by Java Junior View Post
    So if I just change that there's nothing more I need to change within the code? I'll try that in a bit thanks
    Well, you do have to add each individual score to that list ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    rfq
    rfq is offline Member
    Join Date
    Mar 2012
    Location
    Novosibirsk, Russia
    Posts
    15
    Rep Power
    0

    Default 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

  1. HashMap problem
    By vince8864 in forum New To Java
    Replies: 1
    Last Post: 02-13-2012, 11:51 AM
  2. hashmap or search problem
    By flushdabuffer in forum Android
    Replies: 0
    Last Post: 04-09-2011, 01:55 PM
  3. hashmap problem
    By minotaurus in forum Advanced Java
    Replies: 5
    Last Post: 03-16-2011, 11:24 AM
  4. Problem with HashMap
    By maz09 in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 09:40 PM
  5. Iterating through HashMap problem
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-28-2009, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •