Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Arrays in Hashtable

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic