NewBee help with HashMaps

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

  1. psmith

    psmith 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.
     
    psmith, Feb 24, 2007
    #1
    1. Advertisements

  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());
    }
    }
     
    Andreas Beresko, Feb 25, 2007
    #2
    1. Advertisements

  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());
    }
    }
     
    Andreas Beresko, Feb 25, 2007
    #3
  4. psmith

    Lew Guest

    Please do not top post.

    private final String name;
    public Student( String name )
    {
    this.name = name;
    }
    public final String getName()
    {
    return name;
    }
    @Override
    public final String toString()
    {
    return getName();
    }
    tutorGroup.put( name, new Student( name ) );
    // other methods
    - Lew
     
    Lew, Feb 25, 2007
    #4
  5. psmith

    Lew Guest

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

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

    psmith Guest

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