Java Counting # of occurrences of a word in a string

前端 未结 8 1826
再見小時候
再見小時候 2020-12-03 19:20

I have a large text file I am reading from and I need to find out how many times some words come up. For example, the word the. I\'m doing this line by line e

相关标签:
8条回答
  • 2020-12-03 20:18

    To get the number of occurrence of a specific word use the below code

         Pattern pattern = Pattern.compile("Thewordyouwant");
            Matcher matcher = pattern.matcher(string);
            int count = 0;
            while(matcher.find())
                count++;
    
    0 讨论(0)
  • 2020-12-03 20:20
    public class OccurenceOfWords {
     public static void main(String args[]){    
       String file = "c:\\customer1.txt";
       TreeMap <String ,Integer> index = new TreeMap();
    
        String []list = null;
          try(    FileReader fr = new FileReader(file);//using arm jdk 7.0 feature
                    BufferedReader br = new BufferedReader(fr))
            {
                String line = br.readLine();
                while(line!= null){
                    list = line.split("[ \n\t\r:;',.(){}]");
                    for(int i = 0 ; i < list.length;i++)
                    {
                      String word = list[i].toLowerCase();  
                        if(word.length() != 0)
                        {
                            if(index.get(word)== null)
                            { index.put(word,1);
                             }
                            else    
                            {
                                int occur = index.get(word).intValue();
                                occur++;
                                index.put(word, occur);
                            }
                            line = br.readLine();
                        }  
                    }
             }}
                             catch(Exception ex){
                           System.out.println(ex.getMessage());
                           }
                        for(String item : index.keySet()){
                            int repeats = index.get(item).intValue();
                           System.out.printf("\n%10s\t%d",item,repeats);
                     }   
                 }               
      }
    
    0 讨论(0)
提交回复
热议问题