NewBee help with HashMaps - extra

P

psmith

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
 
A

AmiDaniel

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

psmith

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.

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
 
P

psmith

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

Lew

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
 
P

psmith

<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?
 
H

hthukral@sun

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
 
A

AmiDaniel

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)".
 
L

Lew

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
 
P

psmith

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)".

Thanks Daniel - it's working as it should be - 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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top