The example in this article describes the method of using Java to identify the number of occurrences of a certain word in an article. Share it with everyone 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 reading stream FileInputStream fis; try { //The path here needs to be modified according to the specific situation 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 reading 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 character parsing 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."; //Declare and initialize string str1 String str2 = "How to use StringTokenizer?StringTokenizer?"; //Declare and initialize the string str2 StringTokenizer strT1 = new StringTokenizer(str1,",.!"); //Create an object strT1 of the StringTokenizer class, and construct an analyzer for the string str1 //Use space characters, ",", "." and "!" as delimiters StringTokenizer strT2 = new StringTokenizer(str2, "?") ; //Create an object strT2 of the StringTokenizer class, and construct an analyzer for the string str2 //Use space characters and "?" as delimiters int num1 = strT1.countTokens(); //Get the number of language symbols in string str1 int num2 = strT2.countTokens(); //Get the number of language symbols in string str2 System.out.println("str1 has "+ num1+" words.They are:"); while(strT1.hasMoreTokens()) { //Use a loop to get the next language symbol in string str1 and output String str = strT1.nextToken(); System.out.print("/""+str+"/" "); } System.out.println("/nstr2 has "+num2+" words.They are:"); while(strT2 .hasMoreTokens()) { //Use a loop to get the next language symbol in string str2 and output String str = strT2.nextToken(); System.out.print("/""+str+"/" "); } }}I hope this article will be helpful to everyone’s Java programming.