質問

I am fairly new to java. I want to count the occurrences of words in a particular line. So far i can only count the words but no idea how to count occurrences.

Is there a simple way to do this?

Scanner file = new Scanner(new FileInputStream("/../output.txt"));
int count = 0;
  while (file.hasNextLine()) {
    String s = file.nextLine();
    count++;    
      if(s.contains("#AVFC")){
       System.out.printf("There are %d words on this line ", s.split("\\s").length-1);
       System.out.println(count);   
      }

  }
file.close(); 

Output:

    There are 4 words on this line 1

    There are 8 words on this line 13

    There are 3 words on this line 16
役に立ちましたか?

解決

Simplest way I can think of is to use String.split("\\s"), which will split based on spaces.

Then have a HashMap containing a word as the key with the value being the number of times it is used.

   HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>();

      while (file.hasNextLine()) {
        String s = file.nextLine(); 
        String[] words = s.split("\\s");
        int count;
        for (String word : words) {
           if (mapOfWords.get(word) == null) {
              mapOfWords.put(word, 1);
           }
           else {
              count = mapOfWord.get(word);
              mapOfWords.put(word, count + 1);
           }
        }
      }

Implementation you requested to skip strings that contain certain words

   HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>();

   while (file.hasNextLine()) {
        String s = file.nextLine(); 
        String[] words = s.split("\\s");
        int count;

        if (isStringWanted(s) == false) {
           continue;  
        } 

        for (String word : words) {
           if (mapOfWords.get(word) == null) {
              mapOfWords.put(word, 1);
           }
           else {
              count = mapOfWord.get(word);
              mapOfWords.put(word, count + 1);
           }
        }
      }

private boolean isStringWanted(String s) {
    String[] checkStrings = new String[] {"chelsea", "Liverpool", "#LFC"};

    for (String check : checkString) {
        if (s.contains(check)) {
           return false;
        }
    }
    return true;
}

他のヒント

Try below code, it may solve your problem, in addition you can call String.toLowerCase() before you put it into the hashmap

String line ="a a b b b b a q c c";
...
Map<String,Integer> map = new HashMap<String,Integer>();
Scanner scanner = new Scanner(line); 
while (scanner.hasNext()) {
    String s = scanner.next();
    Integer count = map.put(s,1); 
    if(count!=null) map.put(s,count + 1);
}
...
System.out.println(map);

Result:

{b=4, c=2, q=1, a=3}

Fastest would be store the splitted data in a ArrayList then iterate on your ArrayList and use [Collections.frequency] (http://www.tutorialspoint.com/java/util/collections_frequency.htm)

Check Guava's Multiset. Their description starts with 'The traditional Java idiom for e.g. counting how many times a word occurs in a document is something like:'. You find some code snippets how to do that without a MultiSet.

BTW: If you only wanted to count the number of words in your string, why not just count the spaces? You could use StringUtils from the apache commons. It's much better than creating an array of the split parts. Also have a look at their implementation.

int count = StringUtils.countMatches(string, " ");

In a given String, occurrences of a given String can be counted using String#indexOf(String, int) and through a loop

String haystack = "This is a string";
String needle = "i";
int index = 0;

while (index != -1) {
    index = haystack.indexOf(needle, index + 1);

    if (index != -1) {
        System.out.println(String.format("Found %s in %s at index %s.", needle, haystack, index));
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top