I have an array list of expressions and an array list of files I am making a filter for these files; the filter will also be Arraylist ->
Let's say I have an array list with 4 files [file, file, file, file] text files for example and I have these 3 expressions / conditions:
[X, O, Z]
In order to move one of the files into the filter array list, the 3 conditions must be validated in the file
ArrayList<String> XpathExpression= new ArrayList<String>();
ArrayList<File> FilterdFiles= new ArrayList<File>();
File folder = new File("Path for many files");
File[] files = folder.listFiles();
for (File file : files) {
if(file.isFile() && file.getName().endsWith(".txt")) {
//Parser here
/*******Get attribute values******/
for(String xpathExp: XpathExpression) {
if(what I have to write in the if statment to check that the three conditions are valid for the current file so i can add it to the FilterdFiles.) {
FilterdFiles.add(file);
}
else
continue;
}
WHAT I have to write in the if statment to check that the three conditions are valid for the current file so i can add it to the FilterdFiles.
CodePudding user response:
I can't give you a solution based on your code because it doesn't contain enough information. But I can give you an example that should help you solve your problem.
The code below will add strings from a list that contains all strings in another array. I use the flag matched to keep track of matches.
import java.util.*;
public class Main{
public static void main(String[] args) {
String[] files = {"cat and dog", "dog", "fish dog cat", "apple", "peach"};
String[] expressions = {"dog", "cat"};
ArrayList<String> filtered = new ArrayList<>();
for(String f: files){
boolean matched = true;
for(String e: expressions){
if (!f.contains(e)) {
matched = false;
break;
}
}
if (matched) filtered.add(f);
}
System.out.println(filtered);
}
}
Output:
[cat and dog, fish dog cat]
CodePudding user response:
you can write another validate method like this if all the conditions must match:
private boolean validate(File file, ArrayList<String> XpathExpression){
for(String xpathExp: XpathExpression){
boolean validateResult = validate file with xpathExp;
if(validateResult != true){
return false;
}
}
return true;
}
then add file to filter array list if validate(file, XpathExpression) return true.
Or validate if any xpathExp matches:
private boolean validate(File file, ArrayList<String> XpathExpression){
for(String xpathExp: XpathExpression){
boolean validateResult = validate file with xpathExp;
if(validateResult == true){
return true;
}
}
return false;
}
