Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

NewBee help with HashMaps

Thread view: 
psmith@mcwy.com - 24 Feb 2007 23:44 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.