This article illustrates how Java recognizes the number of occurrences of a word in an article. Share to everyone for your reference. Specifically 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;
The try {//here path needs to be modified according to the circumstances FIS = new FileInputStream ("H:\\tankwar1.9\\src\\tank.java");
DataInputStream dis = new DataInputStream (FIS);
String line = null; while (line = Dis.readline ())!= null) {//Create 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 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 do I use StringTokenizer?"
StringTokenizer? ";
Declares and initializes the string str2 stringtokenizer strT1 = new StringTokenizer (str1, ",.!"); Creates an object strT1 for the StringTokenizer class and constructs a string STR1 parser//To spaces, ",", "." and "!"
As delimiter stringtokenizer strT2 = new StringTokenizer (str2, "?"); Creates an object strT2 for the StringTokenizer class and constructs a string STR2 parser//To spaces and "?"
As 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 ()) {//using loops to get the next language symbol in the string str1 and output string str = Strt1.nexttoken ();
System.out.print ("\" "+str+"); } System.out.println ("\NSTR2 has" +num2+ words.)
They are: "); WhileStrt2.hasmoretokens ()) {//Use loops to get the next language symbol in the string str2 and output string str = Strt2.nexttoken ();
System.out.print ("\" "+str+");
}
}
}
I hope this article will help you with your Java programming.