Hi
I am trying to store a list of students with their marks into a
hash table. When i try to show the students list , my marks array is having only the last student data. Rest all lost.
Class Student{
private String name;
private int marks [];
public Student (String _name, String _matricNum, int _marks[])
{
name = _name;
matricNum = _matricNum;
marks = _marks;
}
public int[] getMarks(){
return marks;
}
}
Class clas{
private Hashtable studhash = new Hashtable();
public void registerStudent
(String name, String matricNum, int marks[]){
Student s = new Student (name, matricNum, marks);
studhash.put(s.getName(), s );
}
public void showStudents(){
Collection coll = studhash.values();
Iterator i = coll.iterator();
while (i.hasNext()){
Student s = (Student) i.next();
s.show();
int[] rmarks = s.getMarks();
for (int i = 0; i < rmarks.length; i++){
System.out.println("Mark" + i + " " + rmarks[i]);
}
}
}
}
Public class Main{
String name, matricNum;
Class clas = new Class();
int[] marks = new int[2];
while (cont = true) {
name = getLineFromConsole("Name:");
matricNum = getLineFromConsole("MatricNumber:");
marks[0] = Integer.parseInt(getLineFromConsole("Marks1"));
marks[1] = Integer.parseInt(getLineFromConsole("Marks2"));
clas.registerStudent(name,matricNum,marks);
}
clas.showStudents();
}
thanks in advance