Calculating frequency of each word in a sentence in java

前端 未结 19 2067
夕颜
夕颜 2020-11-29 10:15

I am writing a very basic java program that calculates frequency of each word in a sentence so far i managed to do this much

import java.io.*;

class Linked         


        
19条回答
  •  攒了一身酷
    2020-11-29 10:58

    Determine the frequency of words in a file.

    File f = new File(fileName);
    Scanner s = new Scanner(f);
    Map counts =
     new Map(); 
    while( s.hasNext() ){
     String word = s.next();
    if( !counts.containsKey( word ) )
     counts.put( word, 1 );
    else
     counts.put( word, 
      counts.get(word) + 1 );
    

    }

提交回复
热议问题