import javax.xml.transform.Result; import java.util.Arrays; public class ParseText {//需要统计的字符串为private String text="Abstract-This paper presents an overview";private Result[] res;private int count;public ParseText(){res=new Result[100];count=0;}public void clean(){//数据清洗String flag="-.:;";for(int i=0;i<flags.length();i++){text=text.replace(flags.charAt(i)+".");}}//统计text中每次单词出现的次数public void parse(){//获取text中所有单词String[] words=text.split("\\s+");//为每个单词设置统计标志:true代表已经统计,false代表未被统计boolean[]flags=new boolean[words.length];for (int i=0;i<words.length;i++){flags[i]=false;}//统计每个单词出现的次数for (int i=0;i<words.length;i++){//统计words[i]在后续元素中出现的次数if (flags[i]==false){//表示尚未统计int times=1;for (int j=i+1;j<words.length;j++){if (flags[j]==false&&words[i].equals(words[j])){times++;flags[j]=true;}}//经统计结果写入Result[]数组res[count++]=new Result(words[i],times);}} }public void printResult(){Result[] tmp=new Result[count];tmp=Arrays.copyOfRange(res,0,count);//排序Arrays.sort(tmp);//输出结果for (int i=0;i<count;i++){System.out.println(tmp[i].getWords()+":"+tmp[i].getTimes());}}public static void main(String[] args){ParseText pt=new ParseText();pt.clean();pt.parse();pt.printResult();} }