+ 1

How to write a method that reads particular content in an array lists and count the number of array's content

I am a new learner and I should create a method that will make this program work with no problems at all so I can get the final result that should look like this: 3 is 3 Mark is Mark Richard is Richard Here is the code (PLEASE read the comments I wrote in the code) public class Main { /*I wrote the following method (Student) but I keep get some issues: Please help I spend many days and I can't figure it out and I am runnung out of time as I should understand the problem or at least the correction that I can read and figure out what I was doing wrong.*/ // My written code starts here public static String Student(String[] sx){ int counter = 0; for (int i = 0; i < sx.length; i ++){ if (sx[i] != null) counter ++; } return counter; sx = new String[counter]; } // My written code ENDS here // From this point I should preserve the code without any changes static Student studentA; static Student studentB; static Student studentC; public static void main(String[] args) { studentA = new Student("Mark", "John", "Jimmy"); studentB = new Student("Will", "George", "Androw"); studentC = new Student("Frank", "Sam"); int totalStudents = Student.getTotalStudents(); System.out.println(totalStudents + " is 3"); System.out.println(studentA.getFirstName() + " is Mark"); studentA.setFirstName("Richard"); System.out.println(studentA.getFirstName() + " is Richard"); } }

10th May 2020, 6:19 AM
Mohammad Ala Tahhan
Mohammad Ala Tahhan - avatar
6 odpowiedzi
+ 2
Mohammad Ala Tahhan You can do like this import java.util.*; public class Program { public static void main(String[] args) { Student a = new Student("Anant"); Student b = new Student("AJ"); List<Student> l = new ArrayList<Student>(); l.add(a); l.add(b); System.out.println("No of Student = " + l.size()); for(Student s : l) { System.out.println("Student Name : " + s.getName()); } } } class Student { private String name; public Student(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } }
10th May 2020, 8:00 AM
A͢J
A͢J - avatar
+ 5
Mohammad Ala Tahhan You can make a list of objects and get the Number of students. ArrayList<Student> sl = new ArrayList<Student>(); sl.add(studentA); sl.add(studentB); sl.add(studentC); int totalStudents = sl.size();
10th May 2020, 6:39 AM
A͢J
A͢J - avatar
+ 1
Thanks a lot for you time and explanation dear AJ #Infinity Love.
10th May 2020, 12:50 PM
Mohammad Ala Tahhan
Mohammad Ala Tahhan - avatar
0
Thanks AJ #Infinity Love It seems helpful but I really don't know how to apply that, and I still don't know what did I do wrong in my code! I can't really think right now and I don't have much time to figure out how to apply your solution
10th May 2020, 7:16 AM
Mohammad Ala Tahhan
Mohammad Ala Tahhan - avatar
0
Yes Rithea this is my whole code
10th May 2020, 12:51 PM
Mohammad Ala Tahhan
Mohammad Ala Tahhan - avatar
0
Nice
11th May 2020, 9:21 AM
Samsad Sk
Samsad Sk - avatar