Reputation: 53
My question is kinda similar to these two here;
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());
}
Upvotes: 0
Views: 131
Reputation: 24527
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());
}
Upvotes: 3
Reputation: 919
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.
Upvotes: 2
Reputation: 25873
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.
Upvotes: 1