0

My question is kinda similar to these two here;

  1. pass array to method Java, and
  2. How to pass an Array and the current index to a method in Java

but I'm hitting a wall with the answers provided as I only needed to print the data of the array from a method.

Here is my code:

    public static void main(String[] args) {
    Student[] s1 = {new Student("Karen", "male", 121, "N", 2, "e"),
            new Student("K", "male", 122, "e", 3, "e"),
            new Student("L", "male", 123, "", 4, ""),
            new Student("M", "male", 124, "", 5, ""),
            new Student("O", "female", 125, "", 1, "")};

    s1.studentInfo(); //My questions involves something like this

}

where the set-up is like this

new Student(String StudentName, String Gender, int StudentNumber, String program, int YearLevel, String Orgs)

and this is the method where I want the array data to be accessible, and then prints it.

    public void studentInfo(){
    System.out.println("Student name: "+super.getName());
    System.out.println("Gender: "+super.getGender());
    System.out.println("Student number: "+super.getStudNumber());
    
    System.out.println("Program: "+getProgram());
    System.out.println("Year Level: "+getYrlevel());
    System.out.println("Organization: "+getOrg());
}
Joey
  • 53
  • 1
  • 9

3 Answers3

3

Just iterate over the array and pass the elements to the studentInfo method:

public static void main(String[] args) {
    Student[] s1 = {new Student("Karen", "male", 121, "N", 2, "e"),
            new Student("K", "male", 122, "e", 3, "e"),
            new Student("L", "male", 123, "", 4, ""),
            new Student("M", "male", 124, "", 5, ""),
            new Student("O", "female", 125, "", 1, "")};

    for(Student s : s1) {
        studentInfo(s);
    }

}

public static void studentInfo(Student s){
    System.out.println("Student name: "+s.getName());
    System.out.println("Gender: "+s.getGender());
    System.out.println("Student number: "+s.getStudNumber());
    
    System.out.println("Program: "+s.getProgram());
    System.out.println("Year Level: "+s.getYrlevel());
    System.out.println("Organization: "+s.getOrg());
}
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
  • I tried `s1.studentInfo(s);` earlier, but I only got an error that says _Cannot invoke studentInfo(Student) on the array Student[]_ and IntelliSense is saying that it has to be `s1.length`. – Joey Mar 05 '21 at 12:47
  • Ah, sorry, had a typo. I should have been simply `studentInfo(s)` without `s1` – Benjamin M Mar 05 '21 at 15:34
  • I think your earlier idea is much better: create a `Student#studentInfo()` and call it from the loop. Of course a better name would be `printStudentInfo()`. – m0skit0 Mar 05 '21 at 16:07
  • I'd say it's personal preference and also depends on the use case. It really doesn't make a huge difference if the method is only called in one place. – Benjamin M Mar 05 '21 at 16:19
  • this answer also worked on a static method, thank you. – Joey Mar 06 '21 at 02:11
2

First define method like

public void studentInfo(){
    System.out.println("Student name: "+this.getName());
    System.out.println("Gender: "+this.getGender());
    System.out.println("Student number: "+this.getStudNumber());
    
    System.out.println("Program: "+this.getProgram());
    System.out.println("Year Level: "+this.getYrlevel());
    System.out.println("Organization: "+this.getOrg());
}

in Student class. Then you may use anywhere you need:

Stream.of(s1).forEach(Student::studentInfo)

Where s1 is your students array.

S. Kadakov
  • 861
  • 1
  • 6
  • 15
1

Looks like what you want is something like this:

private static void studentInfo(final Student[] students) {
    for (final Student student : students) {
        System.out.println("Student name: "+ student.getName());
        System.out.println("Gender: "+ student.getGender());
        System.out.println("Student number: "+ student.getStudNumber());

        System.out.println("Program: "+ student.getProgram());
        System.out.println("Year Level: "+ student.getYrlevel());
        System.out.println("Organization: "+ student.getOrg());
    }
}

And you just call it like this:

studentInfo(s1);

Also note that parameter names should start with lower-case as per Java naming conventions.

m0skit0
  • 25,268
  • 11
  • 79
  • 127