NewBee help with HashMaps

P

psmith

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

Andreas Beresko

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());
}
}
 
A

Andreas Beresko

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());
}
}
 
L

Lew

Please do not top post.

Andreas said:
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 said:
public void addStudent( String name ) {

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

// other methods

- Lew
 
L

Lew

Andreas said:
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

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

- Lew
 
P

psmith

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top