-1

I have large ArrayList of Strings. Each String element of the list is quite large too. I want to count how many times the word "the" appears in each element of the list. My current code only iterates through index 1. How do I make it count for all elements of the array?

public static int counter(List<String> comments) {
    for (String comment : comments.subList(1, comments.size() - 1)) {

        String a[] = comment.split(" ");

        String word = "the";
        int count = 0;
        for (int j = 0; j < a.length; j++) {

            if (word.equals(a[j])) {
                count++;
            }
        }
        System.out.println(comment);
        return count;
    }
    System.out.println("sefsfsfseesfeseeeeeeeeeeeeeeeeeeeeeee");
    return 0;
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Do you want separate counts for each element, or a total count across the whole list? The main problem so far seems to be the return inside the for loop, you stop the method immediately after the first element. – Malte Hartwig May 30 '18 at 13:41
  • I wish to get total count across whole list. Index 0 is null, which is why I use sub list. – dinosaurjohnson May 30 '18 at 13:47
  • Possible duplicate of [Count occurrences of words in ArrayList](https://stackoverflow.com/questions/5211194/count-occurrences-of-words-in-arraylist) – GBlodgett May 30 '18 at 13:48
  • I suggest that you read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for some tips to debug your own code. You have made a small error and these tips should help you find it. – Code-Apprentice May 30 '18 at 13:49
  • See how to skip first index in for loop https://stackoverflow.com/questions/22112212/how-to-skip-the-first-iteration-in-a-for-each-loop – isaace May 30 '18 at 13:53

2 Answers2

1

Do not call subList and return after iterate the whole list:

public static int counter(List<String> comments) {
    int count = 0;
    String word = "the";
    for (String comment : comments) {
        String a[] = comment.split(" ");
        for (int j = 0; j < a.length; j++) {
            if (word.equals(a[j])) {
                count++;
            }
        }
        System.out.println(comment);
    }
    System.out.println("sefsfsfseesfeseeeeeeeeeeeeeeeeeeeeeee");
    return count;
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • This is a step in the right direction. However, when I use a for each loop to print out each comment in the list, I copy and pasted the output into word and searched for "the" and it amounts to 423. The counter method only shows 248. Whats going on here? – dinosaurjohnson May 30 '18 at 13:54
  • @dinosaurjohnson I updated the answer. Maybe there are some words like `they`, you can search it. But it will not be counted. – xingbin May 30 '18 at 13:56
  • You may have to convert both strings to lower by testing just the and not The this may be missing out ‘The’ , ‘THe’ – Shyam Joshi May 30 '18 at 13:57
  • aw man, microsoft word is obviously counting "the" that appears in "their" etc, foolish mistake. – dinosaurjohnson May 30 '18 at 14:17
1

The type of your method is not correct instead it should be Map<String, Long> If you are using Java 8 you can create a map of each word and its frequency like this :

Map<String, Long> result = comments.stream()
    .flatMap(comment -> Stream.of(comment.split("\\s+")))
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Then if you want how much the appears you can use :

String word = "the";
Long count = result.get(word);

Or Just :

Long count = comments.stream()
        .flatMap(comment -> Stream.of(comment.split("\\s+")))
        .filter(s -> s.equals(word))
        .count();
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140