0

I'm writing a program which goes through elements in an array list (classlist1), applies a method from another class (Class 2) which returns a string, such that if the string is equal to "yes" the element is added to another array list(classlist2).

Field:

private Class3 file;
private ArrayList<Class2> classlist;

Constructor:

public Class1(String file_name)
file = new Class3(file_name);
classlist1 = ArrayList<Class2>();
classlist1.add(file.file_lines());    //where file_lines is a method from Class3

public Class1(String file_name)
file = new Class3(file_name);
classlist1 = ArrayList<Class2>();
classlist1.add(file.file_lines());    //where file_lines is a method from Class3

Method in question:

public ArrayList<Class2> ()
ArrayList<Class2> classlist2 = new ArrayList<Class2>();
for (Class2 class_details: classlist1){
      // method() is a method in Class2 that returns string
      if (class_details.method() == "yes"){
                classlist2.add(class_details)
      }
}
return classlist2;

What happens: when i run the program it returns an empty list even though there are two elements in classlist1 which class_details.method() = "yes". when i changed the if statement condition to class_details.method() != "yes", it returned a copy of classlist1.

I have no clue what to do next... or instead

  • Use `a.equals(b)` for objects such as strings, not `a == b`. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839#513839 – passer-by Apr 16 '22 at 03:22

0 Answers0