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 - 25 Feb 2007 01:03 GMT
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.
[quoted text clipped - 39 lines]
>
> Thanks in advance.
Andreas Beresko - 25 Feb 2007 01:08 GMT
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.
[quoted text clipped - 39 lines]
>
> Thanks in advance.
Lew - 25 Feb 2007 02:58 GMT
Please do not top post.
> 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 - 25 Feb 2007 03:00 GMT
Andreas Beresko wrote:
>> class Student {
>
[quoted text clipped - 27 lines]
> // other methods
>> }
You don't need the class TutorGroup. Just use the Map tutorGroup.
- Lew
psmith@mcwy.com - 25 Feb 2007 11:15 GMT
> Andreas Beresko wrote:
>
[quoted text clipped - 35 lines]
>
> - Show quoted text -
Lew & Andreas - thanks very much for your help - very much appreciated.