今天要求统计下一篇文章中的单词数以及字母出现情况。一开始我选在在原来一篇自己的博客上修改原来的代码,但是改着改着就突然发现越改越乱,越改越乱。后来听老师的建议:还不如自己重新写一个呢。确实有时候自己重新洗一个程序比看懂原来的程序更简单,
统计单词的这个很轻松,将读入的字符串装换位字符数组再进行相应的判断。
package Word; import java.io.IOException; import java.nio.file.Paths; import java.util.Scanner; public class Word2 { public static void main(String[] args) throws IOException { Find [] word; word=new Find [100]; word[0]=new Find(); word[0].input(word); word[0].show(word); //word[0].outfile(word); } } class Find{ private String word; //单词 private int num; //单词次数 Find(){ word=null; num=1; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } void addnum() { num++; } void input(Find word[]) throws IOException{ //将文件录入并进行标点符号舍去 Scanner in = new Scanner(Paths.get("word.txt")); String k; while(in.hasNextLine()) { String s=in.next(); compare(s,word); //标点符号的舍去以及对单词额比较和录入方法的电泳 } } private void compare(String str1, Find[] word) { char s1[]=str1.toCharArray(); for(int i=0;i<str1.length();i++) { if((s1[i]>=65&&s1[i]<=90)||(s1[i]>=97&&s1[i]<=122))change(s1[i],word); } } private void change(char c, Find[] word) { boolean f=false; int j=0; for(int i=0;word[i].getWord()!=null;i++) { if(word[i].getWord().equals(""+c))word[i].addnum(); if(i==word.length) { f=true; j=i; } } if(f==true) { word[j]=new Find(); word[j].setWord(""+c); } } public void show(Find[] word) { //输出结果 int i=0; for(i=0;i<100;i++) { if(word[i]!=null) { System.out.println(word[i].getWord()+" "+word[i].getNum()); } } } }
统计单词这个主要在于字符串截取上:
package Word;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
public class Code {
public static void main(String[] args) {
WordFind3[] word=new WordFind3[999999];
for(int i=0;i<word.length;i++) {
word[i]=new WordFind3();
}
word[0].input(word);
word[0].show(word);
}
}
class WordFind3{
private String word;
private double num;
private static double time=0;
public WordFind3() {
word=null;
num=1;
}
public static double getTime() {
return time;
}
public static void setTime(double time) {
WordFind3.time = time;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public double getNum() {
return num;
}
public void setNum(double num) {
this.num = num;
}
public void addnum() {
num++;
}
public void input(WordFind3[] word) {
String str;
try {
BufferedReader in=new BufferedReader(new FileReader("word.txt"));
while((str = in.readLine())!=null) {
change(str,word);
}
in.close();
}
catch (IOException e) {
System.out.println("缓冲区错误");
}
}
private void change(String str, WordFind3[] word2) {
char[] s1=str.toCharArray();
String str1=null;
int star=0,end=0;
boolean f=false;
for(int i=0;i<s1.length;i++) {
if((s1[i]>90&&s1[i]<97)||s1[i]<65||s1[i]>122) {
if(star==0&&end==0) {
star=i;
continue;
}
else {
end=i;
}
if(star==0)continue;
//System.out.println(star+" "+end);
str1=str.substring(star+1,end);
time++;
add(str1,word2);
star=end;
}
}
}
private void add(String s, WordFind3[] word2) {
int j=0;
boolean f=false;
for(int i=0;i<word2.length;i++) {
if(word2[i].getWord()==null) {f=true;j=i;break;}
if(word2[i].getWord().equals(s)) {word2[i].addnum();break;}
}
if(f==true) {
word2[j]=new WordFind3();
word2[j].setWord(s);
word2[j].setNum(1);
}
}
void show(WordFind3[] word2) {
DecimalFormat df = new DecimalFormat( "0.0000");
double b=0;
for(int i=0;word2[i].getWord()!=null;i++) {
if(word2[i].getWord().equals("")) {
word2[i].setTime(word2[i].getTime()-word2[i].getNum());
}
}
for(int i=0;word2[i].getWord()!=null;i++) {
if(word2[i].getWord().equals(""))continue;
b=word2[i].getNum()/word2[i].getTime();
System.out.println(word2[i].getWord()+" "+df.format(b));//
}
}
}