aspose file tools*
The moose likes Java in General and the fly likes Arrays in Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Arrays in Hashtable" Watch "Arrays in Hashtable" New topic
Author

Arrays in Hashtable

thiags
Greenhorn

Joined: Jun 20, 2001
Posts: 5
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"thiags",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Arrays in Hashtable
 
Similar Threads
Re: Urgent with this program, Pls help
Adding object to array
toString method and output
Question on ArrayList
help in fixing the error