Reputation: 23
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
Upvotes: 0
Views: 310
Reputation: 21124
The indexOf
returns an int which is the index within this string of the first occurrence of the specified substring, starting at the specified index. But filter requires a predicate, which should return a boolean. So the compiler gives an error. Here's the corrected version.
long countWords = stringStream2.flatMap(str -> Stream.of(str.split("[ ,.!?\n]")))
.filter(str -> str.length() > x && str.charAt(x) == s.charAt(0))
.count();
Upvotes: 2
Reputation: 79075
Given below is an example of how to do it:
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
String s = "l";
List<String> list = List.of(
"Hello world! Good morning. What is clay? Mr. Holger, Mrs. Potter and others went to see the place.");
long count = list.stream()
.flatMap(str -> Stream.of(str.split("[ ,.!?\n]")))
.filter(str -> str.length() >= 1 && str.substring(1, 2).equals(s))
.count();
System.out.println("Count of all the words with the second character equal to '" + s + "' is " + count);
}
}
Output:
Count of all the words with the second character equal to 'l' is 2
Alternatively, you can useString::charAt
as follows:
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
char c = 'l';
List<String> list = List.of(
"Hello world! Good morning. What is clay? Mr. Holger, Mrs. Potter and others went to see the place.");
long count = list.stream()
.flatMap(str -> Stream.of(str.split("[ ,.!?\n]")))
.filter(str -> str.length() >= 1 && str.charAt(1) == c)
.count();
System.out.println("Count of all the words with the second character equal to '" + c + "' is " + count);
}
}
Output:
Count of all the words with the second character equal to 'l' is 2
Upvotes: 1
Reputation: 40034
Here is one way. There are many. You may want to adjust the regex
in split. I just did it for spaces.
The key is to use indexOf()
and supply a string to look for the character. Since you were just looking for a character of some positive length I used the String.isEmpty()
to look for non empty strings.
int post = 2; // character position to look for
long count = 0;
try {
count = Files.lines(Paths.get(fileName))
.flatMap(line -> Stream.of(line.split("\\s+")))
.filter(word -> !word.isEmpty() && word.indexOf("s") == pos)
.count();
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("Count = " + count);
Upvotes: 1