How to return a new ArrayList?

dre2008

New Member
I have an ArrayList: \[code\]ArrayList<Student> studentList = new ArrayList<Student>();\[/code\]Which is populated from a text file, with five pieces of information per object. One of the pieces of information is the "grade". I want to print all the students in a specific grade. My current method only shows the first instance of the student with in the grade. With "[]" around that one. \[code\]public ArrayList<Student> studentInGrade(String category) { ArrayList<Student> gradeCategory = new ArrayList<Student>(); for (Student stu : studentList ) { if (stu.GetCategory().toUpperCase().contains(category.toUpperCase())) { System.out.println("Found"); gradeCategory .add(stu ); return gradeCategory ; } } System.out.println("No Category Found"); return null;}\[/code\]Example: I want to see all the students who are "junior".Example Input:\[code\]Johnny JohnsComputer ScienceJunior21In-StateAsheley AshersNursingSophomore20In-StateAndrew AndersBasket WeavingGraduate Student18Out-StateMorgan FreemanTheaterJunior21In-State\[/code\]What I want to output:\[code\]Johnny JohnsComputer ScienceJunior21In-StateMorgan FreemanTheaterJunior21In-State\[/code\]What outputs currently:\[code\][Johnny JohnsComputer ScienceJunior21In-State]\[/code\]
 
Back
Top