NewBee help with HashMaps

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

  1. Guest

    I am using BlueJ and trying to create a HashMap to hold students names
    and grade results.

    I have been told that one variable called students, should hold a map
    of students using their names as keys. The student objects which are
    the values of this map are to represent the students in the the tutor
    group.

    So far I declared the variables - not sure if I have done this
    correctly:

    private String students;
    private char results;

    Firstly should the String student should be an array?

    I have my constructor:

    public TutorGroup()
    {

    Map<String, Student> tutorGroup=new HashMap<String, Student>();

    }

    My problem starts here:

    public void addStudent(String name)
    {

    tutorGroup.put(Student, name); // error I get here is cannot
    find variable tutorGroup.
    }

    I have to use the above to add a Students name to the student object.
    I have put various bits of code into the method above, but just
    constantly get error messages.

    Can anybody please help me with what I should be doing above, or point
    my in a direction to try and read and understand where I am going
    wrong.

    Thanks in advance.
     
    , Feb 24, 2007
    #1
    1. Advertising

  2. 1. You need to declarate tutorGroup as member variable of your class...

    2. You try to .put(Student,name) in your HashMap<String, Student>, but
    you should better .put(name, Student> in your map (as declared)...

    3. No design award...

    class Student{

    }


    class TutorGroup {

    Map<String, Student> tutorGroup;

    public TutorGroup() {

    tutorGroup = new HashMap<String, Student>();

    }

    public void addStudent(String name) {

    tutorGroup.put(name,new Student());
    }
    }





    > I am using BlueJ and trying to create a HashMap to hold students names
    > and grade results.
    >
    > I have been told that one variable called students, should hold a map
    > of students using their names as keys. The student objects which are
    > the values of this map are to represent the students in the the tutor
    > group.
    >
    > So far I declared the variables - not sure if I have done this
    > correctly:
    >
    > private String students;
    > private char results;
    >
    > Firstly should the String student should be an array?
    >
    > I have my constructor:
    >
    > public TutorGroup()
    > {
    >
    > Map<String, Student> tutorGroup=new HashMap<String, Student>();
    >
    > }
    >
    > My problem starts here:
    >
    > public void addStudent(String name)
    > {
    >
    > tutorGroup.put(Student, name); // error I get here is cannot
    > find variable tutorGroup.
    > }
    >
    > I have to use the above to add a Students name to the student object.
    > I have put various bits of code into the method above, but just
    > constantly get error messages.
    >
    > Can anybody please help me with what I should be doing above, or point
    > my in a direction to try and read and understand where I am going
    > wrong.
    >
    > Thanks in advance.
    >
     
    Andreas Beresko, Feb 25, 2007
    #2
    1. Advertising

  3. 1. You need to declare tutorGroup as a member variable of your
    TutorGroup class to access it in method addStudent()...

    2. You try to .put(Student,name) into your map but you have to
    ..put(name, Student) into it, because you declared your map as
    <String,Student>...

    3. No design award...

    class Student{

    }


    class TutorGroup {

    Map<String, Student> tutorGroup;

    public TutorGroup() {

    tutorGroup = new HashMap<String, Student>();

    }

    public void addStudent(String name) {

    tutorGroup.put(name,new Student());
    }
    }

    > I am using BlueJ and trying to create a HashMap to hold students names
    > and grade results.
    >
    > I have been told that one variable called students, should hold a map
    > of students using their names as keys. The student objects which are
    > the values of this map are to represent the students in the the tutor
    > group.
    >
    > So far I declared the variables - not sure if I have done this
    > correctly:
    >
    > private String students;
    > private char results;
    >
    > Firstly should the String student should be an array?
    >
    > I have my constructor:
    >
    > public TutorGroup()
    > {
    >
    > Map<String, Student> tutorGroup=new HashMap<String, Student>();
    >
    > }
    >
    > My problem starts here:
    >
    > public void addStudent(String name)
    > {
    >
    > tutorGroup.put(Student, name); // error I get here is cannot
    > find variable tutorGroup.
    > }
    >
    > I have to use the above to add a Students name to the student object.
    > I have put various bits of code into the method above, but just
    > constantly get error messages.
    >
    > Can anybody please help me with what I should be doing above, or point
    > my in a direction to try and read and understand where I am going
    > wrong.
    >
    > Thanks in advance.
    >
     
    Andreas Beresko, Feb 25, 2007
    #3
  4. Lew Guest

    Please do not top post.

    Andreas Beresko wrote:

    > class Student {


    private final String name;
    public Student( String name )
    {
    this.name = name;
    }
    public final String getName()
    {
    return name;
    }
    @Override
    public final String toString()
    {
    return getName();
    }
    >
    > }
    >
    >
    > class TutorGroup {
    >

    Map<String, Student> tutorGroup = new HashMap<String, Student>();

    > public void addStudent( String name ) {


    tutorGroup.put( name, new Student( name ) );

    > }


    // other methods
    > }


    - Lew
     
    Lew, Feb 25, 2007
    #4
  5. Lew Guest

    Andreas Beresko wrote:
    >
    >> class Student {

    >
    > private final String name;
    > public Student( String name )
    > {
    > this.name = name;
    > }
    > public final String getName()
    > {
    > return name;
    > }
    > @Override
    > public final String toString()
    > {
    > return getName();
    > }
    >>
    >> }
    >>
    >>
    >> class TutorGroup {
    >>

    > Map<String, Student> tutorGroup = new HashMap<String, Student>();
    >
    >> public void addStudent( String name ) {

    >
    > tutorGroup.put( name, new Student( name ) );
    >
    >> }

    >
    > // other methods
    >> }


    You don't need the class TutorGroup. Just use the Map tutorGroup.

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

    On 25 Feb, 03:00, Lew <> wrote:
    > Andreas Beresko wrote:
    >
    > >> class Student {

    >
    > > private final String name;
    > > public Student( String name )
    > > {
    > > this.name = name;
    > > }
    > > public final String getName()
    > > {
    > > return name;
    > > }
    > > @Override
    > > public final String toString()
    > > {
    > > return getName();
    > > }

    >
    > >> }

    >
    > >> class TutorGroup {

    >
    > > Map<String, Student> tutorGroup = new HashMap<String, Student>();

    >
    > >> public void addStudent( String name ) {

    >
    > > tutorGroup.put( name, new Student( name ) );

    >
    > >> }

    >
    > > // other methods
    > >> }

    >
    > You don't need the class TutorGroup. Just use the Map tutorGroup.
    >
    > - Lew- Hide quoted text -
    >
    > - Show quoted text -


    Lew & Andreas - thanks very much for your help - very much appreciated.
     
    , Feb 25, 2007
    #6
    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. Maps and Hashmaps

    , May 25, 2004, in forum: Java
    Replies:
    2
    Views:
    2,467
    jAnO!
    May 25, 2004
  2. Marilyn Hart

    Help on Hashmaps

    Marilyn Hart, Jan 22, 2005, in forum: Java
    Replies:
    5
    Views:
    651
    Bjorn Abelli
    Jan 23, 2005
  3. Replies:
    1
    Views:
    319
    Oliver Wong
    Feb 27, 2007
  4. Replies:
    10
    Views:
    522
    AmiDaniel
    Feb 28, 2007
  5. Replies:
    1
    Views:
    498
    Tom Hawtin
    Mar 11, 2007
Loading...

Share This Page