NewBee help with HashMaps - extra

Discussion in 'Java' started by psmith, Feb 25, 2007.

  1. psmith

    psmith Guest

    Ok - I am really stuck on this. I have managed to create a Hashmap
    that holds student names and will hold scores of coursework, and it
    will finally give them a grade.

    I now have to:


    /**
    * Asks the user to enter the marks for all the students for the given
    tma.
    * It should check that the tma number provided as the argument is
    valid
    * but it can assume that the marks entered by the user are valid.
    */


    I am getting no where - error messages all the time. Can anybody
    point me in the right direction.
    Firstly my two classes are Student and TutorGroup. When I create a
    new TutorGroup (using blueJ) - I can see attribute - students.
    click
    on students - it shows key (students name) - type java.util.map -
    click on the student and I can see their respective details :
    attribute (name) with its value"Billy"(type string) and attribute
    (tma) with is value 0(int).


    How do I use this line:


    public void enterMarksForTMA(int tma)


    to complete the above.


    I have received no end of error messages. I have tried a keySet but
    have only managed to get it to display Name[0,0]X instead of just
    their name.


    Any help is greatly appreciated.


    Thanks.


    Paul
     
    psmith, Feb 25, 2007
    #1
    1. Advertisements

  2. psmith

    AmiDaniel Guest

    It's very difficult to tell exactly what you're trying to do here--
    what do you mean by "click on the student"? Perhaps if you could
    provide some source code of your methods I might be able to figure out
    what you're doing wrong.
    Just a shot in the dark, are you using something to the effect of
    "System.out.println(MyHashMap.keySet)"? The "keySet" field of the
    HashMap is itself a set of keys, using this code will print out the
    result of the "toString()" method of the HashMap's keySet field. To
    print out each of the keys you'll need to something like the
    following:

    for (String s : MyHashMap.keySet) {
    System.out.println(s);
    }

    Again, if you can provide some source code, I may be able to help a
    bit more.
     
    AmiDaniel, Feb 26, 2007
    #2
    1. Advertisements

  3. psmith

    psmith Guest

    Hi,

    Sorry what I meant by 'clicking on student etc...' - was when I was
    inspecting the variables via BlueJ.

    My method and code at the moment is:

    /**
    * Asks the user to enter the marks for all the students for the
    given tma.
    * It should check that the tma number provided as the argument is
    valid
    * but it can assume that the marks entered by the user are valid.
    */
    public void enterMarksForTMA(int tma)
    {
    int tmaMark = tma;
    if (tma <1 || tma > Student.NUMBER_OF_TMAS)
    {
    OUDialog.alert("Sorry that is not the correct TMA number");
    }
    else
    {
    //Set<String> name = students.keySet();
    //for (String eachStudent: name)
    //{
    tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " + tma
    + " mark for "));// + Student.getName());
    OUDialog.alert("You scored " + tmaMark + " in your last
    paper");
    //tmaMark = Student.setMark(tma, tmaMark);
    //}

    }

    When I used the keySet I was given the full details of the Student
    instead of just their name. I have // where I need the students name
    - I get error messages stating that it is a non-static method.

    I also need to be able to record the tmaMark against the student,
    again within the map, and again I am getting non-static method errors.

    Any help, code/pushing in the right direction greatly appreciated.

    Paul
     
    psmith, Feb 26, 2007
    #3
  4. psmith

    psmith Guest

    Hi,

    Sorry what I meant by 'clicking on student etc...' - was when I was
    inspecting the variables via BlueJ.


    My method and code at the moment is:


    /**
    * Asks the user to enter the marks for all the students for the given
    tma.
    * It should check that the tma number provided as the argument is
    valid
    * but it can assume that the marks entered by the user are valid.
    */
    public void enterMarksForTMA(int tma)
    {
    int tmaMark = tma;
    if (tma <1 || tma > Student.NUMBER_OF_TMAS)
    {
    OUDialog.alert("Sorry that is not the correct TMA number");
    }
    else
    {
    for (String eachStudent: students.keySet()) - //GOT THIS
    FROM YOUR MESSAGE ABOVE
    //{
    tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " +
    tma + " mark for " + eachStudent));

    //tmaMark = Student.setMark(tma, tmaMark); //NOW STUCK HERE
    //}

    }

    With your message above I was able to correctly generate the name for
    each student. THANK YOU

    So now how do I get it to register the entered tmaMark against the
    student within the map? I am getting non-static method error
    messages.

    Any help, code/pushing in the right direction greatly appreciated.


    Paul
     
    psmith, Feb 26, 2007
    #4
  5. psmith

    Lew Guest

    <http://www.physci.org/codes/sscce.html>

    - Lew
     
    Lew, Feb 26, 2007
    #5
  6. psmith

    psmith Guest

    Ok,

    Hooefully this will make it easier.

    I have two classes: TutorGroup and Student.

    In Student I have a method - public void setMark(int tma, int mark).

    In TutorGroup I have another method - public void enterMarksForTMA(int
    tma)

    In the TutorGroup method I have a dialog box which requests you enter
    the mark for the tma(argument).
    Once entered this method needs to update the Student method - public
    void setMark(int tma, int mark), which is within a Map set called
    students, with the two numbers entered from the TutorGroup method.

    I have put:

    Student.setMark(int tma, int mark) = new setMark(tma,
    tmaMark);

    but get a class error message. Any ideas?
     
    psmith, Feb 26, 2007
    #6
  7. hi,

    my sincere advice to you is to clear your fundamentals in Data
    Structure in java ,especially in the Hash Map , there u will find the
    complete comprehensive approach to use hash maps
    u know this time we people gonna help you ..but to be a sole
    programmer its your battle , your choice of tools and weapons (in
    java).
    thanks
     
    [email protected], Feb 26, 2007
    #7
  8. psmith

    AmiDaniel Guest

    Firstly, Student is, I presume, a class--not an individual instance of
    the class. "setMark(int, int)" is a non-static method of this class,
    meaning that the method is associated with instances of Student as
    opposed to the class itself. The instances of your Student class are
    likely stored as values in your HashMap. Following should, I believe,
    do what you want:

    for (String eachStudent : students.keySet()) {

    try {
    int tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " +
    tma +
    " mark for " + eachStudent));
    tmaMark = students.get(eachStudent).setMark(tma, tmaMark);
    } catch (NumberFormatException e)
    { OUDialog.alert(e.toString()); }
    }

    This is presuming that "students" is a HashMap<String, Student>, and
    that the Student class has a method "public int setMark(int, int)".
     
    AmiDaniel, Feb 26, 2007
    #8
  9. psmith

    Lew Guest

    Nope. Still not an SSCCE.
    setMark() is "within" in what way? Is it a Map or a Set? What is the type of
    'students'?
    new setMark( blah, blah) is constructor syntax for a class called 'setMark'.
    Do you have such a class?
    "a class error message" is rather vague. /Copy and paste/ the /exact/ message
    so we can know /exactly/ what you are talking about.

    You will get a compiler error if 'setMark' is not a class in your classpath.

    Please read
    <http://www.physci.org/codes/sscce.html>

    - Lew
     
    Lew, Feb 26, 2007
    #9
  10. psmith

    psmith Guest

    Thanks Daniel - it's working as it should be - very much appreciated.
     
    psmith, Feb 27, 2007
    #10
  11. psmith

    AmiDaniel Guest

    Not a problem--glad I could help.
     
    AmiDaniel, Feb 28, 2007
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.