ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

飘2

2021-12-31 12:37:07  阅读:224  来源: 互联网

标签: sz run map1 words new string2


package total;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;


class Statistics_words {
public Map<String, Integer> map1 = new HashMap<String, Integer>();

public static void main(String arg[]) throws IOException {
String sz[];
Integer num[];
final int MAXNUM = 20;

sz = new String[MAXNUM + 1];
num = new Integer[MAXNUM + 1];
Statistics_words Statistics_words = new Statistics_words();
int account = 1;
// Vector<String> ve1=new Vector<String>();
Statistics_words.textImport();
System.out.println("文本出现单词的次数情况为:");
int g_run = 0;

for (g_run = 0; g_run < MAXNUM + 1; g_run++) {
account = 1;
for (Map.Entry<String, Integer> it : Statistics_words.map1.entrySet()) {
if (account == 1) {
sz[g_run] = it.getKey();
num[g_run] = it.getValue();
account = 2;
}
if (account == 0) {
account = 1;
continue;
}
if (num[g_run] < it.getValue()) {
sz[g_run] = it.getKey();
num[g_run] = it.getValue();
}
// System.out.println("英文单词: "+it.getKey()+" 该英文单词出现次数: "+it.getValue());
}
Statistics_words.map1.remove(sz[g_run]);
}
int g_count = 1;
String tx1 = new String();
for (int i = 0; i < g_run; i++) {
if (sz[i] == null)
continue;
if (sz[i].equals(""))
continue;
tx1 += "出现次数第" + (g_count) + "多的单词为:" + sz[i] + "\t\t\t出现次数: " + num[i] + "\r\n";
System.out.println("出现次数第" + (g_count) + "多的单词为:" + sz[i] + "\t\t\t出现次数: " + num[i]);
g_count++;
}
Statistics_words.textExport(tx1);

}

public void textImport() throws IOException {

File a = new File("C:\\Users\\12903\\Desktop\\a.txt");
FileInputStream b = new FileInputStream(a);
InputStreamReader c = new InputStreamReader(b, "UTF-8");
String string2 = new String();
while (c.ready()) {
char string1 = (char) c.read();
if (!isWord(string1)) {
if (map1.containsKey(string2)) {
Integer num1 = map1.get(string2) + 1;
map1.put(string2, num1);
} else {
Integer num1 = 1;
map1.put(string2, num1);
}
string2 = "";
} else {
string2 += string1;
}
}
if (!string2.isEmpty()) {
if (map1.containsKey(string2)) {
Integer num1 = map1.get(string2) + 1;
map1.put(string2, num1);
} else {
Integer num1 = 1;
map1.put(string2, num1);
}
string2 = "";
}
c.close();
b.close();
}

public void textExport(String txt) throws IOException {
File fi = new File("StatisticsWord.txt");
FileOutputStream fop = new FileOutputStream(fi);
OutputStreamWriter ops = new OutputStreamWriter(fop, "UTF-8");
ops.append(txt);
ops.close();
fop.close();
}

public boolean isWord(char a) {
if (a <= 'z' && a >= 'a' || a <= 'Z' && a >= 'A')
return true;
return false;
}

}

标签:,sz,run,map1,words,new,string2
来源: https://www.cnblogs.com/20203986liudekai/p/15752585.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有