Find specific word in text file and count it

2020-03-02 03:17发布

someone can help me with code? How to search in text file any word and count how many it were repeated?

For example test.txt:

hi
hola
hey
hi
bye
hoola
hi

And if I want to know how many times are repeated in test.txt word "Hi" program must say "3 times repeated"

I hope you understood what I want, thank you for answers.

13条回答
我命由我不由天
2楼-- · 2020-03-02 03:25
public class Wordcount 
{
   public static void main(String[] args)
   {       
       int count=0;

       String str="hi this is is is line";

       String []s1=str.split(" ");

       for(int i=0;i<=s1.length-1;i++)
       {
          if(s1[i].equals("is"))
           {
               count++; 
           }
       }

       System.out.println(count);
   }
}
查看更多
3楼-- · 2020-03-02 03:25
package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public  class Test {

    public static void main(String[] args)  throws Exception{

        BufferedReader bf= new BufferedReader(new FileReader("src/test.txt"));
        Scanner sc = new Scanner(System.in);
        String W=sc.next();
        //String regex ="[\\w"+W+"]";
        int count=0;

        //Pattern p = Pattern.compile();
        String line=bf.readLine();
        String s[];
        do
        {
            s=line.split(" ");
            for(String a:s)
            {
                if(a.contains(W))
                    count++;

            }


            line=bf.readLine();


        }while(line!=null);
        System.out.println(count);
    }



}
查看更多
聊天终结者
4楼-- · 2020-03-02 03:28

Use MultiSet collection from google guava library.

Multiset<String> wordsMultiset = HashMultiset.create();
Scanner scanner = new Scanner(fileName);
while (scanner.hasNextLine()) {
    wordsMultiset.add(scanner.nextLine());
}
for(Multiset.Entry<String> entry : wordsMultiset ){
     System.out.println("Word : "+entry.getElement()+" count -> "+entry.getCount());
}
查看更多
倾城 Initia
5楼-- · 2020-03-02 03:28

You can read text file line by line. I assume that each line can contain more than one word. For each line, you call:

String[] words = line.split(" "); 
for(int i=0; i<words.length; i++){
   if(words[i].equalsIgnoreCase(searhedWord))
         count++;
}
查看更多
够拽才男人
6楼-- · 2020-03-02 03:33
import java.io.*;
import java.util.*;

class filedemo
{
public static void main(String ar[])throws Exception
BufferedReader br=new BufferedReader(new FileReader("c:/file.txt"));
 System.out.println("enter the string which you search");
 Scanner ob=new Scanner(System.in);
 String str=ob.next();
 String str1="",str2="";
 int count=0;
while((str1=br.readLine())!=null)
 {
 str2 +=str1;

}  

 int index = str2.indexOf(str);

 while (index != -1) {
 count++;
 str2 = str2.substring(index + 1);
 index = str2.indexOf(str);
}

System.out.println("Number of the occures="+count);
}
}  
查看更多
男人必须洒脱
7楼-- · 2020-03-02 03:34
public int countWord(String word, File file) {
int count = 0;
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
    String nextToken = scanner.next();
    if (nextToken.equalsIgnoreCase(word))
    count++;
}
return count;
}
查看更多
登录 后发表回答