Question

I'm having a difficult time figuring how to display the shared personArray with the appropriate course object. For example, I need to display a list of a course and the people that are in that course, and again for the next course. I feel that there's something wrong with my nested for loop or the personCount variable because not every course has the same amount of people so how do I deal with this?

Hopefully you all can get a clear picture of the stripped down code below.

Note* I must use aggregation and toStrings, and not allowed to use lists, arraylists, or scanner.

Thanks!

public class School {

    private static Course[] courseArray = new Course[5];

    public static void printList(){
        String print = "";
            for (int x = 0; x < courseCount; x++) {
                print += courseArray[x].getCourseName() + "\n";

                        for (int y = 0; y < personCount; y++){
                            print += courseArray[y].personArray[y].getPersonName()+ "\n";
                                               //^ I thought I could use the 1st forloop index to stay in the 1 course and print out all the people for that course(like the hardcode example below) but I get a nullpointer
                        }
                    }
                    JOptionPane.showMessageDialog(null,print);

/*      
  If I hardcode the print String like below I get the correct output. 
  So I know my objects are being stored properly, but I can't figure out how to get the correct display with a loop         
    print +=  courseArray[0].personArray[0].getPersonName(); //tim (bio)
    print +=  courseArray[0].peopleArray[1].getPersonName(); //bob (bio)
    print +=  courseArray[1].peopleArray[2].getPersonName(); //john (art)
*/          

    }                   
}
------------------------
public class Course { 

    private Person[] personArray = new Person[50]; 

    //constructor for course

    // get/set courseName methods

    public String toString(){
        // not sure how to use this the right way
    }
}
-----------------------------------
public class Person extends Course {

    //constructor for person

    // get/set personName methods

    public String toString(){
        return getPersonName();
    } 
}
Was it helpful?

Solution

Make sure that all 50 of the people in your personArray are initialized first; check to see if they are by printing out the entire array like so (you will need to import java.util.Arrays):

System.out.println(Arrays.toString(course[x].personArray));

If that gives back any "null" values then you know that not every single Person in the PersonArray was initialized first.

OTHER TIPS

You should change courseCount to courseArray.length and personCount to courseArray.getPersonArraySize() and implement getPersonArraySize() in Course class to return length of personArray.

replace your loops with these below..

for (Course c : courseArray) {

   print += c.getCourseName() + "\n";

   for (Person p : c.personArray){

     print += p.getPersonName()+ "\n";

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top