I count words from a file
try(Stream<String> stringStream2 = Files.lines(Paths.get(fileName))){
String s = "l";
int x = 0;
long countWords = stringStream2
.flatMap(str -> Stream.of(str.split("[ ,.!?\n]")))
.filter(str -> str.length()> x && str.contains(s))
.filter(str -> str.indexOf(s,2))
.count();
System.out.println(countWords);
}
catch (IOException e){
e.printStackTrace();
}
I am trying to use an indexOf(), but it`s not working. if i am using charAt() or indexOf() then there will be a error. I don’t understand how to count all the words with the second character equal to "x" for example using Stream API