Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > NewBee help with HashMaps - extra

Reply
Thread Tools

NewBee help with HashMaps - extra

 
 
psmith@mcwy.com
Guest
Posts: n/a
 
      02-25-2007
Ok - I am really stuck on this. I have managed to create a Hashmap
that holds student names and will hold scores of coursework, and it
will finally give them a grade.

I now have to:


/**
* Asks the user to enter the marks for all the students for the given
tma.
* It should check that the tma number provided as the argument is
valid
* but it can assume that the marks entered by the user are valid.
*/


I am getting no where - error messages all the time. Can anybody
point me in the right direction.
Firstly my two classes are Student and TutorGroup. When I create a
new TutorGroup (using blueJ) - I can see attribute - students.
click
on students - it shows key (students name) - type java.util.map -
click on the student and I can see their respective details :
attribute (name) with its value"Billy"(type string) and attribute
(tma) with is value 0(int).


How do I use this line:


public void enterMarksForTMA(int tma)


to complete the above.


I have received no end of error messages. I have tried a keySet but
have only managed to get it to display Name[0,0]X instead of just
their name.


Any help is greatly appreciated.


Thanks.


Paul

 
Reply With Quote
 
 
 
 
AmiDaniel
Guest
Posts: n/a
 
      02-26-2007
On Feb 25, 12:14 pm, (E-Mail Removed) wrote:
>
> I am getting no where - error messages all the time. Can anybody
> point me in the right direction.
> Firstly my two classes are Student and TutorGroup. When I create a
> new TutorGroup (using blueJ) - I can see attribute - students.
> click
> on students - it shows key (students name) - type java.util.map -
> click on the student and I can see their respective details :
> attribute (name) with its value"Billy"(type string) and attribute
> (tma) with is value 0(int).


It's very difficult to tell exactly what you're trying to do here--
what do you mean by "click on the student"? Perhaps if you could
provide some source code of your methods I might be able to figure out
what you're doing wrong.

> I have received no end of error messages. I have tried a keySet but
> have only managed to get it to display Name[0,0]X instead of just
> their name.


Just a shot in the dark, are you using something to the effect of
"System.out.println(MyHashMap.keySet)"? The "keySet" field of the
HashMap is itself a set of keys, using this code will print out the
result of the "toString()" method of the HashMap's keySet field. To
print out each of the keys you'll need to something like the
following:

for (String s : MyHashMap.keySet) {
System.out.println(s);
}

Again, if you can provide some source code, I may be able to help a
bit more.

--
Daniel Cannon

 
Reply With Quote
 
 
 
 
psmith@mcwy.com
Guest
Posts: n/a
 
      02-26-2007
On 26 Feb, 03:46, "AmiDaniel" <(E-Mail Removed)> wrote:
> On Feb 25, 12:14 pm, (E-Mail Removed) wrote:
>
>
>
> > I am getting no where - error messages all the time. Can anybody
> > point me in the right direction.
> > Firstly my two classes are Student and TutorGroup. When I create a
> > new TutorGroup (using blueJ) - I can see attribute - students.
> > click
> > on students - it shows key (students name) - type java.util.map -
> > click on the student and I can see their respective details :
> > attribute (name) with its value"Billy"(type string) and attribute
> > (tma) with is value 0(int).

>
> It's very difficult to tell exactly what you're trying to do here--
> what do you mean by "click on the student"? Perhaps if you could
> provide some source code of your methods I might be able to figure out
> what you're doing wrong.
>
> > I have received no end of error messages. I have tried a keySet but
> > have only managed to get it to display Name[0,0]X instead of just
> > their name.

>
> Just a shot in the dark, are you using something to the effect of
> "System.out.println(MyHashMap.keySet)"? The "keySet" field of the
> HashMap is itself a set of keys, using this code will print out the
> result of the "toString()" method of the HashMap's keySet field. To
> print out each of the keys you'll need to something like the
> following:
>
> for (String s : MyHashMap.keySet) {
> System.out.println(s);
>
> }
>
> Again, if you can provide some source code, I may be able to help a
> bit more.
>
> --
> Daniel Cannon


Hi,

Sorry what I meant by 'clicking on student etc...' - was when I was
inspecting the variables via BlueJ.

My method and code at the moment is:

/**
* Asks the user to enter the marks for all the students for the
given tma.
* It should check that the tma number provided as the argument is
valid
* but it can assume that the marks entered by the user are valid.
*/
public void enterMarksForTMA(int tma)
{
int tmaMark = tma;
if (tma <1 || tma > Student.NUMBER_OF_TMAS)
{
OUDialog.alert("Sorry that is not the correct TMA number");
}
else
{
//Set<String> name = students.keySet();
//for (String eachStudent: name)
//{
tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " + tma
+ " mark for "));// + Student.getName());
OUDialog.alert("You scored " + tmaMark + " in your last
paper");
//tmaMark = Student.setMark(tma, tmaMark);
//}

}

When I used the keySet I was given the full details of the Student
instead of just their name. I have // where I need the students name
- I get error messages stating that it is a non-static method.

I also need to be able to record the tmaMark against the student,
again within the map, and again I am getting non-static method errors.

Any help, code/pushing in the right direction greatly appreciated.

Paul

 
Reply With Quote
 
psmith@mcwy.com
Guest
Posts: n/a
 
      02-26-2007
On 26 Feb, 03:46, "AmiDaniel" <(E-Mail Removed)> wrote:
> On Feb 25, 12:14 pm, (E-Mail Removed) wrote:
>
>
>
> > I am getting no where - error messages all the time. Can anybody
> > point me in the right direction.
> > Firstly my two classes are Student and TutorGroup. When I create a
> > new TutorGroup (using blueJ) - I can see attribute - students.
> > click
> > on students - it shows key (students name) - type java.util.map -
> > click on the student and I can see their respective details :
> > attribute (name) with its value"Billy"(type string) and attribute
> > (tma) with is value 0(int).

>
> It's very difficult to tell exactly what you're trying to do here--
> what do you mean by "click on the student"? Perhaps if you could
> provide some source code of your methods I might be able to figure out
> what you're doing wrong.
>
> > I have received no end of error messages. I have tried a keySet but
> > have only managed to get it to display Name[0,0]X instead of just
> > their name.

>
> Just a shot in the dark, are you using something to the effect of
> "System.out.println(MyHashMap.keySet)"? The "keySet" field of the
> HashMap is itself a set of keys, using this code will print out the
> result of the "toString()" method of the HashMap's keySet field. To
> print out each of the keys you'll need to something like the
> following:
>
> for (String s : MyHashMap.keySet) {
> System.out.println(s);
>
> }
>
> Again, if you can provide some source code, I may be able to help a
> bit more.
>
> --
> Daniel Cannon

Hi,

Sorry what I meant by 'clicking on student etc...' - was when I was
inspecting the variables via BlueJ.


My method and code at the moment is:


/**
* Asks the user to enter the marks for all the students for the given
tma.
* It should check that the tma number provided as the argument is
valid
* but it can assume that the marks entered by the user are valid.
*/
public void enterMarksForTMA(int tma)
{
int tmaMark = tma;
if (tma <1 || tma > Student.NUMBER_OF_TMAS)
{
OUDialog.alert("Sorry that is not the correct TMA number");
}
else
{
for (String eachStudent: students.keySet()) - //GOT THIS
FROM YOUR MESSAGE ABOVE
//{
tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " +
tma + " mark for " + eachStudent));

//tmaMark = Student.setMark(tma, tmaMark); //NOW STUCK HERE
//}

}

With your message above I was able to correctly generate the name for
each student. THANK YOU

So now how do I get it to register the entered tmaMark against the
student within the map? I am getting non-static method error
messages.

Any help, code/pushing in the right direction greatly appreciated.


Paul



 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> My method and code at the moment is:
>
>
> /**
> * Asks the user to enter the marks for all the students for the given
> tma.
> * It should check that the tma number provided as the argument is
> valid
> * but it can assume that the marks entered by the user are valid.
> */
> public void enterMarksForTMA(int tma)
> {
> int tmaMark = tma;
> if (tma <1 || tma > Student.NUMBER_OF_TMAS)
> {
> OUDialog.alert("Sorry that is not the correct TMA number");
> }
> else
> {
> for (String eachStudent: students.keySet()) - //GOT THIS
> FROM YOUR MESSAGE ABOVE
> //{
> tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " +
> tma + " mark for " + eachStudent));
>
> //tmaMark = Student.setMark(tma, tmaMark); //NOW STUCK HERE
> //}
>
> }
>
> Any help, code/pushing in the right direction greatly appreciated.


<http://www.physci.org/codes/sscce.html>

- Lew
 
Reply With Quote
 
psmith@mcwy.com
Guest
Posts: n/a
 
      02-26-2007
On 26 Feb, 12:45, Lew <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > My method and code at the moment is:

>
> > /**
> > * Asks the user to enter the marks for all the students for the given
> > tma.
> > * It should check that the tma number provided as the argument is
> > valid
> > * but it can assume that the marks entered by the user are valid.
> > */
> > public void enterMarksForTMA(int tma)
> > {
> > int tmaMark = tma;
> > if (tma <1 || tma > Student.NUMBER_OF_TMAS)
> > {
> > OUDialog.alert("Sorry that is not the correct TMA number");
> > }
> > else
> > {
> > for (String eachStudent: students.keySet()) - //GOT THIS
> > FROM YOUR MESSAGE ABOVE
> > //{
> > tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " +
> > tma + " mark for " + eachStudent));

>
> > //tmaMark = Student.setMark(tma, tmaMark); //NOW STUCK HERE
> > //}

>
> > }

>
> > Any help, code/pushing in the right direction greatly appreciated.

>
> <http://www.physci.org/codes/sscce.html>
>
> - Lew- Hide quoted text -
>
> - Show quoted text -


Ok,

Hooefully this will make it easier.

I have two classes: TutorGroup and Student.

In Student I have a method - public void setMark(int tma, int mark).

In TutorGroup I have another method - public void enterMarksForTMA(int
tma)

In the TutorGroup method I have a dialog box which requests you enter
the mark for the tma(argument).
Once entered this method needs to update the Student method - public
void setMark(int tma, int mark), which is within a Map set called
students, with the two numbers entered from the TutorGroup method.

I have put:

Student.setMark(int tma, int mark) = new setMark(tma,
tmaMark);

but get a class error message. Any ideas?

 
Reply With Quote
 
hthukral@sun
Guest
Posts: n/a
 
      02-26-2007
On Feb 26, 12:14 am, (E-Mail Removed) wrote:
> Ok - I am really stuck on this. I have managed to create a Hashmap
> that holds student names and will hold scores of coursework, and it
> will finally give them a grade.
>
> I now have to:
>
> /**
> * Asks the user to enter the marks for all the students for the given
> tma.
> * It should check that the tma number provided as the argument is
> valid
> * but it can assume that the marks entered by the user are valid.
> */
>
> I am getting no where - error messages all the time. Can anybody
> point me in the right direction.
> Firstly my two classes are Student and TutorGroup. When I create a
> new TutorGroup (using blueJ) - I can see attribute - students.
> click
> on students - it shows key (students name) - type java.util.map -
> click on the student and I can see their respective details :
> attribute (name) with its value"Billy"(type string) and attribute
> (tma) with is value 0(int).
>
> How do I use this line:
>
> public void enterMarksForTMA(int tma)
>
> to complete the above.
>
> I have received no end of error messages. I have tried a keySet but
> have only managed to get it to display Name[0,0]X instead of just
> their name.
>
> Any help is greatly appreciated.
>
> Thanks.
>
> Paul


hi,

my sincere advice to you is to clear your fundamentals in Data
Structure in java ,especially in the Hash Map , there u will find the
complete comprehensive approach to use hash maps
u know this time we people gonna help you ..but to be a sole
programmer its your battle , your choice of tools and weapons (in
java).
thanks

 
Reply With Quote
 
AmiDaniel
Guest
Posts: n/a
 
      02-26-2007
On Feb 26, 3:37 am, (E-Mail Removed) wrote:
>
> So now how do I get it to register the entered tmaMark against the
> student within the map? I am getting non-static method error
> messages.


Firstly, Student is, I presume, a class--not an individual instance of
the class. "setMark(int, int)" is a non-static method of this class,
meaning that the method is associated with instances of Student as
opposed to the class itself. The instances of your Student class are
likely stored as values in your HashMap. Following should, I believe,
do what you want:

for (String eachStudent : students.keySet()) {

try {
int tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " +
tma +
" mark for " + eachStudent));
tmaMark = students.get(eachStudent).setMark(tma, tmaMark);
} catch (NumberFormatException e)
{ OUDialog.alert(e.toString()); }
}

This is presuming that "students" is a HashMap<String, Student>, and
that the Student class has a method "public int setMark(int, int)".

--
Daniel Cannon

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-26-2007
Lew wrote:
>> <http://www.physci.org/codes/sscce.html>


(E-Mail Removed) wrote:
> Hooefully this will make it easier.


Nope. Still not an SSCCE.

> I have two classes: TutorGroup and Student.
>
> In Student I have a method - public void setMark(int tma, int mark).
>
> In TutorGroup I have another method - public void enterMarksForTMA(int
> tma)
>
> In the TutorGroup method I have a dialog box which requests you enter
> the mark for the tma(argument).
> Once entered this method needs to update the Student method - public
> void setMark(int tma, int mark), which is within a Map set called
> students, with the two numbers entered from the TutorGroup method.


setMark() is "within" in what way? Is it a Map or a Set? What is the type of
'students'?

> I have put:
>
> Student.setMark(int tma, int mark) = new setMark(tma,
> tmaMark);


new setMark( blah, blah) is constructor syntax for a class called 'setMark'.
Do you have such a class?

> but get a class error message. Any ideas?


"a class error message" is rather vague. /Copy and paste/ the /exact/ message
so we can know /exactly/ what you are talking about.

You will get a compiler error if 'setMark' is not a class in your classpath.

Please read
<http://www.physci.org/codes/sscce.html>

- Lew


 
Reply With Quote
 
psmith@mcwy.com
Guest
Posts: n/a
 
      02-27-2007
On 26 Feb, 22:00, "AmiDaniel" <(E-Mail Removed)> wrote:
> On Feb 26, 3:37 am, (E-Mail Removed) wrote:
>
>
>
> > So now how do I get it to register the entered tmaMark against the
> > student within the map? I am getting non-static method error
> > messages.

>
> Firstly, Student is, I presume, a class--not an individual instance of
> the class. "setMark(int, int)" is a non-static method of this class,
> meaning that the method is associated with instances of Student as
> opposed to the class itself. The instances of your Student class are
> likely stored as values in your HashMap. Following should, I believe,
> do what you want:
>
> for (String eachStudent : students.keySet()) {
>
> try {
> int tmaMark = Integer.parseInt(OUDialog.request("Enter TMA " +
> tma +
> " mark for " + eachStudent));
> tmaMark = students.get(eachStudent).setMark(tma, tmaMark);
> } catch (NumberFormatException e)
> { OUDialog.alert(e.toString()); }
>
> }
>
> This is presuming that "students" is a HashMap<String, Student>, and
> that the Student class has a method "public int setMark(int, int)".
>
> --
> Daniel Cannon


Thanks Daniel - it's working as it should be - very much appreciated.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NewBee: HashMaps and an Array psmith@mcwy.com Java 1 03-11-2007 09:39 PM
NewBee help with HashMaps psmith@mcwy.com Java 1 02-27-2007 03:55 PM
NewBee help with HashMaps psmith@mcwy.com Java 5 02-25-2007 11:15 AM
Help on Hashmaps Marilyn Hart Java 5 01-23-2005 03:51 AM
Maps and Hashmaps neelu111@hotmail.com Java 2 05-25-2004 01:47 PM



Advertisments