toString Iteration of an ArrayList
Posted by kevaughn
Last Updated: May 27, 2014
  1149

public class School {
    
    private String schoolName;
    private String schoolLocation;
    private ArrayList <Student> listOfStudents;
    private ArrayList <Instructor> listOfInstructors;

    public School(String schoolName, String schoolLocation, ArrayList<Student> listOfStudents, ArrayList<Instructor> listOfInstructors) {
        this.schoolName = schoolName;
        this.schoolLocation = schoolLocation;
        this.listOfStudents = listOfStudents;
        this.listOfInstructors = listOfInstructors;
    }
    public String getSchoolName() {
        return schoolName;
    }

    public String getSchoolLocation() {
        return schoolLocation;
    }

    public ArrayList<Student> getListOfStudents() {
        return listOfStudents;
    }

    public ArrayList<Instructor> getListOfInstructors() {                             
        return listOfInstructors;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public void setSchoolLocation(String schoolLocation) {
        this.schoolLocation = schoolLocation;
    }

    public void setListOfStudents(ArrayList<Student> listOfStudents) {
        this.listOfStudents = listOfStudents;
    }

    public void setListOfInstructors(ArrayList<Instructor> listOfInstructors) {
        this.listOfInstructors = listOfInstructors;
    }

    @Override
    public String toString() {
        return "school Name: \t\t" + schoolName + ", school Location: \t\t" + schoolLocation + ", "
                + "list Of Students: \t\t" + listOfStudents + 
                
                ", list Of Instructors: \t\t" + listOfInstructors;
    }
     
   
}
the toString method needs to iterate through the list of students and the list of instructors and include their information in the returned string. 

That's the instruction but mi know mi could use the for loop with index to do it. But should i list put the name of the students/instructors and them loop through them using ..has.Next. Let me know or see an example of the above code. Respect.
   
  
 
 
   

 
 
Here is the correct code.....

 @Override
    public String toString() {
       String result = "";
       String resultStudent="";
       String resultInstuctor="";
       
        
        for (Student stu : listOfStudents) 
        {
          resultStudent = "First name: "+stu.getFirstname()+"\n"+"Last name: "+stu.getLastname()+"\n"+"Address: "+stu.getAddress()+"ID: "+stu.getstudentID()+"\n"+"Major: "+stu.getmajor();
        }
        
        for (Instructor instuc : listOfInstructors) 
        {
          resultStudent = "First name: "+instuc.getFirstname()+"\n"+"Last name: "+instuc.getLastname()+"\n"+"Address: "+instuc.getAddress()+"Office Hours: "+instuc.getOfficeHours();
        }
        
        result= resultStudent +" \n"+ resultInstuctor;
        
        return result;
    }