Java method to identify the number of occurrences of a word in an article

  • 2020-04-01 04:14:11
  • OfStack

This article illustrates a Java method for recognizing the number of occurrences of a word in an article. Share with you for your reference. The details are as follows:

1. Java code:


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Select {
  public static void main(String[] args) {
    int num = 0;
    //Definition: byte read stream
    FileInputStream fis;
    try {
      //The path here needs to be modified on a case-by-case basis
      fis = new FileInputStream("H:\TankWar1.9\src\Tank.java");
      DataInputStream dis = new DataInputStream(fis);
      String line = null;
      while ((line = dis.readLine()) != null) {
        //Create a character parser
         StringTokenizer st=new StringTokenizer(line,"!&(){}+-= ':;<> /");
         while(st.hasMoreTokens()) { 
           String string=st.nextToken();
           if(string.equals("if")) { num++; } }
        ;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println(num);
  }
}

2. Select. Java:


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Select {
  public static void main(String[] args) {
    int num = 0;
    //Definition: byte read stream
    FileInputStream fis;
    try {
      fis = new FileInputStream("H:\TankWar1.9\src\Tank.java");
      DataInputStream dis = new DataInputStream(fis);
      String line = null;
      while ((line = dis.readLine()) != null) {
        //Create a character resolution class
         StringTokenizer st=new StringTokenizer(line,"!&(){}+-= ':;<> /");
         while(st.hasMoreTokens()) { 
           String string=st.nextToken();
           if(string.equals("if")) { num++; } }
        ;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println(num);
  }
}

3. StringTokenizerDemo. Java:


import java.util.*;
public class StringTokenizerDemo
{
  public static void main(String[] args)
  {
      String str1 = "Hello world!This is Java code,stringTokenizer Demo.";
      //Declares and initializes the string str1
      String str2 = "How to use StringTokenizer?StringTokenizer?";
      //Declares and initializes the string str2
      StringTokenizer strT1 = new StringTokenizer(str1," ,.!");
      //Creates the object of the StringTokenizer class, strT1, and constructs the parser for the string str1
      //With Spaces,",","." and "!" As a delimiter
      StringTokenizer strT2 = new StringTokenizer(str2," ?");
      //Creates strT2, the object of the StringTokenizer class, and constructs the parser for the string str2
      //With Spaces and "?" As a delimiter
      int num1 = strT1.countTokens();
      //Gets the number of language symbols in the string str1
      int num2 = strT2.countTokens();
      //Gets the number of language symbols in the string str2
      System.out.println("str1 has "+num1+" words.They are:");
      while(strT1.hasMoreTokens())
      {  //A loop is used to get the next language symbol in the string str1 and output it
          String str = strT1.nextToken();
          System.out.print("""+str+"" ");
      }
      System.out.println("nstr2 has "+num2+" words.They are:");
      while(strT2.hasMoreTokens())
      {  //A loop is used to get the next language symbol in the string str2 and output it
          String str = strT2.nextToken();
          System.out.print("""+str+"" ");
      }
  }
}

I hope this article has been helpful to your Java programming.


Related articles: