0

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();

}
Edoardo Tavilla
  • 523
  • 2
  • 7
  • 18
  • 2
    `sc.toString()` -> [Scanner::toString](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#toString--) is this really what u meant for there? Also `sc.toString() == excludeWordsList.get(i)` note that [`==` is working not as you expect it to work for strings](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-and-equals-in-java)... – dbl Jul 08 '19 at 09:58
  • I modify into: if (sc.next().equals(excludeWordsList.get(i))), thanks! but it doesn't work too! – Edoardo Tavilla Jul 08 '19 at 10:39
  • It's not the main point of your question, but note that `FileNotFoundException` is a subclass of `IOException`, and you declare you throw `IOException`, so your internal `try/catch` is redundant. https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html – Adam Burke Jul 08 '19 at 11:28

1 Answers1

0

You are using == to compare strings. It's incorrectly - you should use equals(). Also you can refactor your code using lambdas features:

public String removeOtherWords(File targetFile, String excludingWords) throws IOException {
    List<String> lines = Files.readAllLines(targetFile.toPath());
    List<String> excludingWordsList = Arrays.asList(excludingWords.split(" "));
    return lines.stream()
            .flatMap(line -> Arrays.stream(line.split(" ")))
            .filter(excludingWordsList::contains)
            .collect(Collectors.joining(" "));
}
Egor
  • 1,334
  • 8
  • 22