本文實例講述了java識別一篇文章中某單詞出現個數的方法。分享給大家供大家參考。具體如下:
1. java代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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 ; //定義:字節讀取流 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 ) { //創建字符解析器 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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 ; //定義:字節讀取流 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 ) { //創建字符解析類 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import java.util.*; public class StringTokenizerDemo { public static void main(String[] args) { String str1 = "Hello world!This is Java code,stringTokenizer Demo." ; //聲明并初始化字符串str1 String str2 = "How to use StringTokenizer?StringTokenizer?" ; //聲明并初始化字符串str2 StringTokenizer strT1 = new StringTokenizer(str1, " ,.!" ); //創建StringTokenizer類的對象strT1,并構造字符串str1的分析器 //以空格符、","、"."及"!"作為定界符 StringTokenizer strT2 = new StringTokenizer(str2, " ?" ); //創建StringTokenizer類的對象strT2,并構造字符串str2的分析器 //以空格符及"?"作為定界符 int num1 = strT1.countTokens(); //獲取字符串str1中語言符號的個數 int num2 = strT2.countTokens(); //獲取字符串str2中語言符號的個數 System.out.println( "str1 has " +num1+ " words.They are:" ); while (strT1.hasMoreTokens()) { //利用循環來獲取字符串str1中下一個語言符號,并輸出 String str = strT1.nextToken(); System.out.print( "\"" +str+ "\" " ); } System.out.println( "\nstr2 has " +num2+ " words.They are:" ); while (strT2.hasMoreTokens()) { //利用循環來獲取字符串str2中下一個語言符號,并輸出 String str = strT2.nextToken(); System.out.print( "\"" +str+ "\" " ); } } } |
希望本文所述對大家的java程序設計有所幫助。