求一个JAVA小程序 要求统计输出的一段英文段落中单词出现的频率 100

要求是在小程序窗口里面输出点一个统计按钮以后进行统计然后输出结果我可以提供一段程序求高手改一下运行成功就给分importjava.util.ArrayList;impor... 要求是在小程序窗口里面输出 点一个统计按钮以后进行统计 然后输出结果
我可以提供一段程序 求高手改一下 运行成功就给分
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Article {

//保存文章的内容
String content;
String[] rawWords;
String[] words;
int[] wordFreqs;
public Article() {
content = " ";
}
public void splitWord(){
final char SPACE = ' ';
content = content.replace('\'', SPACE).replace(',', SPACE).replace('.', SPACE);
content = content.replace('(', SPACE).replace(')', SPACE).replace('-', SPACE);
rawWords = content.split("\\s+"); }
public void countWordFreq() {
Set<String> set = new TreeSet<String>();
for(String word: rawWords){
set.add(word);
}
Iterator ite = set.iterator();
List<String> wordsList = new ArrayList<String>();
List<Integer> freqList = new ArrayList<Integer>();
while(ite.hasNext()){
String word = (String) ite.next();
int count = 0;
for(String str: rawWords){
if(str.equals(word)){
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
words = wordsList.toArray(new String[0]);
wordFreqs = new int[freqList.size()];
for(int i = 0; i < freqList.size(); i++){
wordFreqs[i] = freqList.get(i);
}
}
public void sort() {
class Word{
private String word;
private int freq; public Word(String word, int freq){
this.word = word;
this.freq = freq;
}
}
class WordComparator implements Comparator{

public int compare(Object o1, Object o2) {
Word word1 = (Word) o1;
Word word2 = (Word) o2;
if(word1.freq < word2.freq){
return 1;
}else if(word1.freq > word2.freq){
return -1;
}else{

int len1 = word1.word.trim().length();
int len2 = word2.word.trim().length();

String min = len1 > len2? word2.word: word1.word;
String max = len1 > len2? word1.word: word2.word;

for(int i = 0; i < min.length(); i++){
if(min.charAt(i) < max.charAt(i)){
return 1;
}
}

return 1;

}
}

}

List wordList = new ArrayList<Word>();

for(int i = 0; i < words.length; i++){
wordList.add(new Word(words[i], wordFreqs[i]));
}

Collections.sort(wordList, new WordComparator());

for(int i = 0; i < wordList.size(); i++){
Word wor = (Word) wordList.get(i);

words[i] = wor.word;
wordFreqs[i] = wor.freq;
}

}

public void printResult() {
System.out.println("Total " + words.length + " different words in the content!");

for(int i = 0; i < words.length; i++){
System.out.println(wordFreqs[i] + " " + words[i]);
}
}

public static void main(String[] args) {
Article a = new Article();
a.splitWord();
a.countWordFreq();
a.sort();
a.printResult();
}
}
展开
 我来答
匿名用户
2012-05-24
展开全部
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Article {

// 保存文章的内容
String content;
String[] rawWords;
String[] words;
int[] wordFreqs;

public Article() {
content = " ";
}

public void splitWord() {
final char SPACE = ' ';
content = content.replace('\'', SPACE).replace(',', SPACE)
.replace('.', SPACE);
content = content.replace('(', SPACE).replace(')', SPACE)
.replace('-', SPACE);
rawWords = content.split("\\s+");
}

public void countWordFreq() {
Set<String> set = new TreeSet<String>();
for (String word : rawWords) {
set.add(word);
}
Iterator ite = set.iterator();
List<String> wordsList = new ArrayList<String>();
List<Integer> freqList = new ArrayList<Integer>();
while (ite.hasNext()) {
String word = (String) ite.next();
int count = 0;
for (String str : rawWords) {
if (str.equals(word)) {
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
words = wordsList.toArray(new String[0]);
wordFreqs = new int[freqList.size()];
for (int i = 0; i < freqList.size(); i++) {
wordFreqs[i] = freqList.get(i);
}
}

public void sort() {
class Word {
private String word;
private int freq;

public Word(String word, int freq) {
this.word = word;
this.freq = freq;
}
}
class WordComparator implements Comparator {

public int compare(Object o1, Object o2) {
Word word1 = (Word) o1;
Word word2 = (Word) o2;
if (word1.freq < word2.freq) {
return 1;
} else if (word1.freq > word2.freq) {
return -1;
} else {

int len1 = word1.word.trim().length();
int len2 = word2.word.trim().length();

String min = len1 > len2 ? word2.word : word1.word;
String max = len1 > len2 ? word1.word : word2.word;

for (int i = 0; i < min.length(); i++) {
if (min.charAt(i) < max.charAt(i)) {
return 1;
}
}

return 1;

}
}

}

List wordList = new ArrayList<Word>();

for (int i = 0; i < words.length; i++) {
wordList.add(new Word(words[i], wordFreqs[i]));
}

Collections.sort(wordList, new WordComparator());

for (int i = 0; i < wordList.size(); i++) {
Word wor = (Word) wordList.get(i);

words[i] = wor.word;
wordFreqs[i] = wor.freq;
}

}

public void printResult() {
System.out.println("Total " + words.length
+ " different words in the content!");

for (int i = 0; i < words.length; i++) {
System.out.println(wordFreqs[i] + " " + words[i]);
}
}

public static void main(String[] args) {
Article a = new Article();
a.content = "sssssssssssaadwerqwersssdfsdftrytyum1242357689235cvbbnm,masdfasdfasdfasdf,fasdf.asdfasdfasdfasdfaasdfasdf";
a.splitWord();
a.countWordFreq();
a.sort();
a.printResult();
}
}
运行一下,看是否成功,的确是一个分号出错了!现在已经改好了!
追问
这个不是窗口程序类型原代码啊。。。我要能用小型窗口程序运行的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
丶大秦左庶长
2012-05-24 · TA获得超过1169个赞
知道小有建树答主
回答量:1845
采纳率:0%
帮助的人:617万
展开全部
import java.util.*;

public class List {
public int[] letterList(String bb){
String b = bb.toLowerCase();
int[] test = new int[26];
for(int n=0;n<b.length();n++)
{
char a=b.charAt(n);
int index=a-'a';
if(index<test.length && index>=0){
test[index]++;
}
}
return test;
}

public void printlist(int test[]){
for(int i=0;i<test.length;i++){
System.out.print(test[i]);
}
}
public static void main(String[] args) {
int []chars=new int[100];
String str = args[0].trim();
List list=new List();
chars=list.letterList(str);
int[] flag = list.letterList(str);
System.out.println("String is:" );
list.printlist(flag);

}
}--------------------------------------------------------
上面的代码能实现统计,在命令行,先编译, 再输入 java List ababcdasgf 既可.面板什么的自己加吧。全让别人帮你做了,你很难学会java的。
更多追问追答
追问
在创建面板里面输入面板input 属性是txetfield 但是统计量content是string
input中的值怎么给content呢
追答
输入面板不需要做。
String words=JOptionPane.showInputDialog("input words:");
这样是弹出一个对话框,提示输入字母,不限字数。
你只需要再做一个面板,显示结果就行。
=========================代码如下==============================
=====================================================
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.GridLayout;

public class List extends JFrame{

public int[] letterList(String bb){
String b = bb.toLowerCase();
int[] test = new int[26];
for(int n=0;n=0){
test[index]++;
}
}
return test;
}

public JLabel printlist(int text[]){
JLabel label =new JLabel();
String strs="各字母出现的次数:\n\n";

for(int i=0;i<text.length;i++){
strs += (" "+text[i]);
}
label.setText(""+strs);
return label;
}
public static void main(String[] args) {
int []chars=new int[100];
String str=JOptionPane.showInputDialog("input words:");
List list=new List();
chars=list.letterList(str);
int[] flag = list.letterList(str);

JFrame frame=new JFrame();
frame.add(list.printlist(flag));
frame.setTitle("字母统计");
frame.setSize(420,150);
frame.setLocation(220,320);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}
还有什么,继续追问。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
紫文6L
2012-05-24
知道答主
回答量:51
采纳率:0%
帮助的人:14.6万
展开全部
while(ite.hasNext()){
String word = (String) ite.next();
int count = 0;
for(String str: rawWords){
if(str.equals(word)){
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
这段代码中的int count = 0;的分号是中文格式的,改成英文格式的就OK
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
伤倏臀w
2012-05-23 · TA获得超过1115个赞
知道大有可为答主
回答量:864
采纳率:60%
帮助的人:604万
展开全部
已经改了,小问题,一个分号出错;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Article {

//保存文章的内容
String content;
String[] rawWords;
String[] words;
int[] wordFreqs;
public Article() {
content = " ";
}
public void splitWord(){
final char SPACE = ' ';
content = content.replace('\'', SPACE).replace(',', SPACE).replace('.', SPACE);
content = content.replace('(', SPACE).replace(')', SPACE).replace('-', SPACE);
rawWords = content.split("\\s+"); }
public void countWordFreq() {
Set<String> set = new TreeSet<String>();
for(String word: rawWords){
set.add(word);
}
Iterator ite = set.iterator();
List<String> wordsList = new ArrayList<String>();
List<Integer> freqList = new ArrayList<Integer>();
while(ite.hasNext()){
String word = (String) ite.next();
int count = 0;
for(String str: rawWords){
if(str.equals(word)){
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
words = wordsList.toArray(new String[0]);
wordFreqs = new int[freqList.size()];
for(int i = 0; i < freqList.size(); i++){
wordFreqs[i] = freqList.get(i);
}
}
public void sort() {
class Word{
private String word;
private int freq; public Word(String word, int freq){
this.word = word;
this.freq = freq;
}
}
class WordComparator implements Comparator{

public int compare(Object o1, Object o2) {
Word word1 = (Word) o1;
Word word2 = (Word) o2;
if(word1.freq < word2.freq){
return 1;
}else if(word1.freq > word2.freq){
return -1;
}else{

int len1 = word1.word.trim().length();
int len2 = word2.word.trim().length();

String min = len1 > len2? word2.word: word1.word;
String max = len1 > len2? word1.word: word2.word;

for(int i = 0; i < min.length(); i++){
if(min.charAt(i) < max.charAt(i)){
return 1;
}
}

return 1;

}
}

}

List wordList = new ArrayList<Word>();

for(int i = 0; i < words.length; i++){
wordList.add(new Word(words[i], wordFreqs[i]));
}

Collections.sort(wordList, new WordComparator());

for(int i = 0; i < wordList.size(); i++){
Word wor = (Word) wordList.get(i);

words[i] = wor.word;
wordFreqs[i] = wor.freq;
}

}

public void printResult() {
System.out.println("Total " + words.length + " different words in the content!");

for(int i = 0; i < words.length; i++){
System.out.println(wordFreqs[i] + " " + words[i]);
}
}

public static void main(String[] args) {
Article a = new Article();
a.splitWord();
a.countWordFreq();
a.sort();
a.printResult();
}
}
只要在当前文件下创建一个文本文件,里面可以写一些单词,就可以了
追问
骗分的一边去吧。。。
追答
放屁
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
S40910045
2012-05-24
知道答主
回答量:3
采纳率:0%
帮助的人:4781
展开全部
一个分号出错
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式