栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151153.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号