欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java识别一篇文章中某单词出现个数的方法

时间:2021-01-06 11:21:32|栏目:JAVA代码|点击:

本文实例讲述了java识别一篇文章中某单词出现个数的方法。分享给大家供大家参考。具体如下:

1. 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;
    //定义:字节读取流
    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:

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:

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程序设计有所帮助。

上一篇:Kotlin语法学习-变量定义、函数扩展、Parcelable序列化等简单总结

栏    目:JAVA代码

下一篇:MyBatisPlus 自定义sql语句的实现

本文标题:java识别一篇文章中某单词出现个数的方法

本文地址:http://www.codeinn.net/misctech/41191.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有