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:36

Apache Commons - StringUtils.countMatches()

查看更多
何必那么认真
3楼-- · 2020-03-02 03:37
package somePackage;   
public static void main(String[] args) {

            String path = ""; //ADD YOUR PATH HERE
            String fileName = "test2.txt";
            String testWord = "Macbeth"; //CHANGE THIS IF YOU WANT
            int tLen = testWord.length();
            int wordCntr = 0;
            String file = path + fileName;
            boolean check;

            try{
                FileInputStream fstream = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;        
                //Read File Line By Line
                while((strLine = br.readLine()) != null){                
                    //check to see whether testWord occurs at least once in the line of text
                    check = strLine.toLowerCase().contains(testWord.toLowerCase());
                    if(check){                    
                        //get the line, and parse its words into a String array
                        String[] lineWords = strLine.split("\\s+");                    
                        for(String w : lineWords){
                            //first see if the word is as least as long as the testWord
                            if(w.length() >= tLen){
                                /*
                                1) grab the specific word, minus whitespace
                                2) check to see whether the first part of it having same length
                                    as testWord is equivalent to testWord, ignoring case
                                */
                                String word = w.substring(0,tLen).trim();                                                        
                                if(word.equalsIgnoreCase(testWord)){                                
                                    wordCntr++;
                                }                            
                            }
                        }                    
                    }   
                }            
                System.out.println("total is: " + wordCntr);
            //Close the input stream
            br.close();
            } catch(Exception e){
                e.printStackTrace();
            }
        }
查看更多
\"骚年 ilove
4楼-- · 2020-03-02 03:39

try using java.util.Scanner.

public int countWords(String w, String fileName) {
int count = 0;
Scanner scanner = new Scanner(inputFile);
scanner.useDelimiter("[^a-zA-Z]"); // non alphabets act as delimeters
String word = scanner.next();
if (word.equalsIgnoreCase(w))
    count++;
   return count;
}
查看更多
Evening l夕情丶
5楼-- · 2020-03-02 03:40
public int occurrencesOfHi()
{
    String newText = Text.replace("Hi","");
    return (Text.length() - newText.length())/2;
}
查看更多
Luminary・发光体
6楼-- · 2020-03-02 03:41

Try it this way with Pattern and Matcher.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Dem {

    public static void main(String[] args){

        try {
            File f = new File("d://My.txt");
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
            String s = new String();

            while((s=br.readLine())!=null){

                s = s + s;

            }

            int count = 0;
            Pattern pat = Pattern.compile("it*");
            Matcher mat = pat.matcher(s);

            while(mat.find()){

                  if(mat.find()){

                      mat.start();
                      count++;

                  }

            }

            System.out.println(count);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

}
查看更多
兄弟一词,经得起流年.
7楼-- · 2020-03-02 03:44
HashMap h=new HashMap();                        
FileInputStream fin=new FileInputStream("d:\\file.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(fin));
String n;
while((n=br.readLine())!=null)
{
    if(h.containsKey(n))
    {
    int i=(Integer)h.get(n);
    h.put(n,(i+1));
    }
    else
    h.put(n, 1);
}

now iterate through this map to get the count for each word using each word as a key to the map values

查看更多
登录 后发表回答