0

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

Holger
  • 285,553
  • 42
  • 434
  • 765
grace
  • 23
  • 3
  • 1
    `filter` needs a predicate (a boolean-returning function). Use `str -> str.indexOf(s, 2) != -1` – user May 05 '20 at 14:19
  • 1
    and also `second character` index should be `1` i guess – Ryuzaki L May 05 '20 at 14:21
  • 1
    I suggest having a look at [this answer](https://stackoverflow.com/a/61454421/2711488) regarding streaming over the words of a file. Then combine with the `filter` of [the answer to your question](https://stackoverflow.com/a/61615430/2711488) and `count()`. – Holger May 05 '20 at 15:24

3 Answers3

2

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();
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
1

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

WJS
  • 36,363
  • 4
  • 24
  • 39
1

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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110