View Single Post
  #2 (permalink)  
Old 08-05-2007, 07:54 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
Student[] students = new Student[500]; // All elements of this array are null. for(int j = 0; j < 10; j++) System.out.println("students["+j+"] = " + students[j]); // If the Student class has a no-argument constructor you could // instantiate them all at once: for(int j = 0; j < stuNumb; j++) students[j] = new Student(); // You could also instantiate a student for each new element as you go: for (int i = 0; i < stuNumb; i++) { String stuName = reader.readLine ("Enter the name of sutden #"+ (i+1) +": "); students[i] = new Student(); // Now we have a non-null array element to work with. students[i].setName(stuName); int testNumb = reader.readInt ("Enter the number of tests for "+ students[i].getName() +": "); }
Reply With Quote