I have a method to load a string from a File and return it.
public String loadStopwords(File targetFile) throws IOException {
File fileTo = new File(targetFile.toString());
BufferedReader br;
String appString = null;
try {
br = new BufferedReader(new FileReader(fileTo));
String st;
while((st=br.readLine()) != null){
System.out.println(st);
appString = st;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return appString;
}
I want to pass this String as argument of another method, plus a File; I want to read this File excluding all the words different by the words I passed in the String. E.g. in my file I have ["My house is so beautiful and big"] and I pass the String ["beautiful big green"], I've to save the new String ["beautiful big"]. I tried with this but it doesn't work:
public String removeOtherWords(File targetFile, String excludingWords) {
ArrayList<String> excludeWordsList = new ArrayList<>();
excludeWordsList.addAll(Arrays.asList(excludingWords.split(" ")));
ArrayList<String> wordList = new ArrayList<String>();
try(Scanner sc = new Scanner(new FileInputStream(targetFile))){
while(sc.hasNext()){
for (int i = 0; i < excludeWordsList.size(); i++) {
if (sc.toString() == excludeWordsList.get(i)) {
wordList.add(sc.next());
}
}
}
sc.close();
//sc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return wordList.toString();
}