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());
}
2
  • 2
    s1 is an array, it cannot have a method studentInfo(). Commented Mar 5, 2021 at 11:23
  • You have to loop on the array and then call a method to print the data. Commented Mar 5, 2021 at 11:23

3 Answers 3

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());
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
Ah, sorry, had a typo. I should have been simply studentInfo(s) without s1
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().
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.
this answer also worked on a static method, thank you.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.