2

I made a method that search through an array list of objects. Then if the searchKey is found in the array list it will print this certain item.

Here is how I iterate through the array list if it contains the searchKey, but I just realized that it is impossible to compare a string and an object.

for(int x = 0; x < Student.students.size(); x ++){
 if(Student.students.contains(searchKey))
 System.out.print(Student.students.get(x));
}

Here's how I create the constructor and array list.

String firstName, lastName, course, yearLevel, gender;
 
 Student(String firstName, String lastName, String course, String yearLevel, String gender)
 {
 this.firstName = firstName;
 this.lastName = lastName;
 this.course = course;
 this.yearLevel = yearLevel;
 this.gender = gender;
 }
 static ArrayList<Student> students = new ArrayList<Student>();
asked May 13, 2022 at 6:19
2
  • please share the List instanciation and class of the object into it Commented May 13, 2022 at 6:21
  • 1
    what are you searching for? first name, last name, course, ... or gender? any of these? exact or partial match? Commented May 13, 2022 at 7:32

2 Answers 2

2

You need to compare the one property; also you can use a for-each loop to simplify the code

for(Student s : Student.students){
 if(s.getName().equals(searchKey))
 System.out.print(s);
}

Note :

When you use a condition Student.students.contains(searchKey) in a loop, and it doesn't use the iteration variable that means there is a problem

answered May 13, 2022 at 6:23
4
  • I posted how I create the list. I also try the method you just mentioned, but it wont work as it is still comparing a string to an object Commented May 13, 2022 at 7:09
  • 1
    @SillyBean: No, it's comparing the name of the student to the search key... Commented May 13, 2022 at 7:33
  • You can also use a stream, students.stream().filter(s -> searchKey.equals(s.getName()).forEach(System.out::println). Commented May 13, 2022 at 9:51
  • @SillyBean what is searchKey a string or a Student ? Commented May 14, 2022 at 7:25
1

You haven't defined what 'contains' means here but I'm going to assume that it means a Student contains the key if it appears as a substring in any of its String members. So let's start with a method that does that. We can define this as part of the Student class itself.

public class Student {
 .... other stuff ....
 /**
 * Return true if any of the properties of this Student
 * contain the given substring, false otherwise
 */
 public boolean contains(String s) {
 // consider addressing null cases - omitting for simplicity
 return firstName.contains(s) ||
 lastName.contains(s) ||
 course.contains(s) ||
 yearLevel.contains(s) ||
 gender.contains(s);
 }
}

Now you can iterate over your List and invoke this method to find the matches. Note that you need to handle the case that multiple Students may match a given search key (or none may match). So I would suggest collecting the results in a separate List. One does not generally iterate over Lists via the index. This example uses an enhanced for-loop (aka for-each).

public List<Student> findMatches(List<Student> students, String key) {
 List<Student> found = new ArrayList<>();
 for (Student s : students) {
 if (s.contains(key)) {
 found.add(s);
 }
 }
 return found;
}

This is a good case for using the Stream API.

public List<Student> findMatches(List<Student> students, String key) {
 return students.stream()
 .filter(s -> s.contains(key))
 .collect(Collectors.toList());
}
answered May 13, 2022 at 10:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.