0

Question will be below.

public class University {
ArrayList<Student> students;

public University() {
    students = new ArrayList<Student>();
}

public void addStudent(Student students) {
    this.students.add(students);
}}

public class Student {
String name;
String studentID;
static int studentNumber = 0;

Student(String name, String sID){
    this.name = name;
    this.studentID = sID;
    studentNumber++;
}}

public class TestUniversity {

public static void main(String[] args) {
    University universityRegister = new University(); 
    Student studentRegister = new Student("Rachel Green", "a1234");
    universityRegister.addStudent(studentRegister);
    
    studentRegister = new Student("Monica Geller", "a12345");
    universityRegister.addStudent(studentRegister);
    
    studentRegister = new Student("Ross Geller", "a1111");
    universityRegister.addStudent(studentRegister);
    
    System.out.println("Number of student in University: " + Student.studentNumber);        
}}

A. I created 3 classes, 1.Student, 2.University, 3.UniversityTester, in 3 different files.

B. I created 3 objects of type Studnet, and stored them in the University class as an ArrayList.

I would like to know how I can print the ArrayList of the students including all information from the UniversityTester class? In the future, I will create another object called Stuff and I will store it in the University class as an ArrayList stuffList. Therefore, I don't want to print the students list from class Student.

Positive Navid
  • 2,481
  • 2
  • 27
  • 41
Chen Xu
  • 19
  • 2
  • 2
    It can help you. https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – sopehl Nov 29 '20 at 19:54
  • 1
    You could also add a print method to class Student, printing the data of one student in your desired format. Then loop over universityRegister.students and call the former method for each retrieved student. – Jörg Nov 29 '20 at 20:16

2 Answers2

0

Override toString() method in Student class as follows:

import java.util.StringJoiner;

public class Student {
  String name;
  String studentID;
  static int studentNumber = 0;

  Student(String name, String sID) {
    this.name = name;
    this.studentID = sID;
    studentNumber++;
  }

  @Override
  public String toString() {
    return new StringJoiner(", ", Student.class.getSimpleName() + "[", "]")
        .add("name='" + name + "'")
        .add("studentID='" + studentID + "'")
        .toString();
  }
}

Now you can print the array list as follows in TestUniversity:

System.out.println("Student Details: " + universityRegister.students);
Tushar
  • 670
  • 3
  • 14
  • 1
    Great! If it resolves the issue then accept the answer and give it an upvote. Keep coding!! – Tushar Nov 29 '20 at 21:06
0

Add this to your student class:

  @Override
  public String toString() {
    return String.format("Student: %s , StudentID: %s", name, studentID);
  }

And then you can print the entire array like this:

System.out.println("Students: " universityRegister.students);

//Or like this:

for(Student st: universityRegister.students){
 System.out.println(st);
}
TiagoRibeiro
  • 306
  • 6
  • 24
  • Thanks, I love it! first time for me to see the method String.format(); – Chen Xu Nov 29 '20 at 20:47
  • 1
    Glad for it (: If it resolved your issue, remember to accept this answer on the top left side of it, and give it a positive rating please. Thank you :3 – TiagoRibeiro Nov 29 '20 at 21:03