NewBee help with HashMaps - extra

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

  1. 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
    , Feb 25, 2007
    #1
    1. Advertising

  2. AmiDaniel Guest

    On Feb 25, 12:14 pm, wrote:
    >
    > 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).


    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.

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


    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.

    --
    Daniel Cannon
    AmiDaniel, Feb 26, 2007
    #2
    1. Advertising

  3. Guest

    On 26 Feb, 03:46, "AmiDaniel" <> wrote:
    > On Feb 25, 12:14 pm, wrote:
    >
    >
    >
    > > 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).

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

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


    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
    , Feb 26, 2007
    #3
  4. Guest

    On 26 Feb, 03:46, "AmiDaniel" <> wrote:
    > On Feb 25, 12:14 pm, wrote:
    >
    >
    >
    > > 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).

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

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

    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
    , Feb 26, 2007
    #4
  5. Lew Guest

    wrote:
    > 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
    > //}
    >
    > }
    >
    > Any help, code/pushing in the right direction greatly appreciated.


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

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

    On 26 Feb, 12:45, Lew <> wrote:
    > wrote:
    > > 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
    > > //}

    >
    > > }

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

    >
    > <http://www.physci.org/codes/sscce.html>
    >
    > - Lew- Hide quoted text -
    >
    > - Show quoted text -


    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?
    , Feb 26, 2007
    #6
  7. hthukral@sun Guest

    On Feb 26, 12:14 am, wrote:
    > 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


    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
    hthukral@sun, Feb 26, 2007
    #7
  8. AmiDaniel Guest

    On Feb 26, 3:37 am, wrote:
    >
    > 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.


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

    --
    Daniel Cannon
    AmiDaniel, Feb 26, 2007
    #8
  9. Lew Guest

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


    wrote:
    > Hooefully this will make it easier.


    Nope. Still not an SSCCE.

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


    setMark() is "within" in what way? Is it a Map or a Set? What is the type of
    'students'?

    > I have put:
    >
    > Student.setMark(int tma, int mark) = new setMark(tma,
    > tmaMark);


    new setMark( blah, blah) is constructor syntax for a class called 'setMark'.
    Do you have such a class?

    > but get a class error message. Any ideas?


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

    On 26 Feb, 22:00, "AmiDaniel" <> wrote:
    > On Feb 26, 3:37 am, wrote:
    >
    >
    >
    > > 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.

    >
    > 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)".
    >
    > --
    > Daniel Cannon


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

    On Feb 27, 1:49 am, wrote:
    > On 26 Feb, 22:00, "AmiDaniel" <> wrote:
    >
    > Thanks Daniel - it's working as it should be - very much appreciated.


    Not a problem--glad I could help.

    --
    Daniel Cannon
    AmiDaniel, Feb 28, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marilyn Hart

    Help on Hashmaps

    Marilyn Hart, Jan 22, 2005, in forum: Java
    Replies:
    5
    Views:
    613
    Bjorn Abelli
    Jan 23, 2005
  2. Replies:
    5
    Views:
    424
  3. Replies:
    1
    Views:
    308
    Oliver Wong
    Feb 27, 2007
  4. Replies:
    1
    Views:
    481
    Tom Hawtin
    Mar 11, 2007
  5. mathieu
    Replies:
    3
    Views:
    593
    Bo Persson
    Sep 4, 2009
Loading...

Share This Page