java程序:统计一段英文段落中每个单词出现的次数,这个段落存储在一个字符串变量中

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

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 set = new TreeSet();
for (String word : rawWords) {
set.add(word);
}
Iterator ite = set.iterator();
List wordsList = new ArrayList();
List freqList = new ArrayList();
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();

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();
}
}
运行一下,看是否成功,的确是一个分号出错了!现在已经改好了!

package test;public class Practice{private static void readWord ( String input, String word, int offset, int count ){offset = input.indexOf (word, offset);if (offset != -1){System.out.println (word + " 在第 " + offset + " 个位置出现过.");readWord (input, word, ++offset, ++count);}else{System.out.println (word + " 总共出现了:" + count + " 次.");}}public static void main ( String[] args ){String input = "Look buddy, U got work hard and put yourself to the java, once U learned the heart of java, I can guarantee that U win.";String word = "java";readWord (input, word, 0, 0);}}

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

/**
* 字典类,记录文章中出现过的所有单词及其次数
* @author Administrator
*
*/
public class Dictionary {

private HashMap< String, Integer > dictionary;
private int wordsCount;

/**
* 字典这个类的构造函数
*/
public Dictionary() {
dictionary = new HashMap< String, Integer >();
wordsCount = 0;
}

/**
* 向字典里插入一个单词
* @param word
*/
public void insert( String word ) {
if ( dictionary.containsKey( word ) ) {
int currentCount = dictionary.get( word );
dictionary.put( word, currentCount + 1 );
} else {
dictionary.put( word, 1 );
}
wordsCount++;
}

/**
* 取得字典里所有不同的单词
* @return
*/
public int getDifferentWordsNum() {
return dictionary.size();
}

/**
* 返回字典里的所有单词 * 其出现次数
* @return
*/
public int getAllWordsNum() {
return wordsCount;
}

/**
* 展示字典中存放的所有单词及其出现次数
*/
public void displayDictionary() {
for ( Iterator< String > it = dictionary.keySet().iterator(); it.hasNext(); ) {
String key = it.next();
System.out.print( key );
System.out.print( ": " );
System.out.println( dictionary.get( key ) );
}
}

public static void main( String[] args ) throws Exception {

//这里放置你所说的段落
String passage = "public static void main( String[] args ) {";

Scanner scanner = new Scanner( passage );

Dictionary dict = new Dictionary();

while ( scanner.hasNextLine() ) {
String line =scanner.nextLine();
boolean isBlankLine = line.matches( "\\W" ) || line.length() == 0;
if ( isBlankLine ) {
continue;
}
String[] words = line.split( "\\W" );
for ( String word : words ) {
if ( word.length() != 0 ) {
dict.insert( word );
}
}
}
dict.displayDictionary();
}
}

设计一个JAVA程序,对一个保存英文文章的文本文件进行统计,最后给出每个英文字符及每个标点的出现次数,按出现次数的降幂排列。 你到底想问什么?残阳破晓SHUANG考虑采纳一下。有空到365testing,测评网,

import java.util.Enumeration;
import java.util.Hashtable;

public class MyTest {

private static Object String;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="aabceeee";
char[] c=s.toCharArray();//转换成char[]
Hashtable h=new Hashtable();//用于存储
for(char c1:c) {
if(h.containsKey(c1))//判断是否已有了
((Counter)h.get(c1)).i++;//有了的话i++
else{
h.put(c1, new Counter());//没有的话存储到h
}
}
Enumeration e=h.keys();//keys的集合
while(e.hasMoreElements()){
Character ra= (Character) e.nextElement();
System.out.println(ra + "=" + ((Counter)h.get(ra)).i);//打印
}
}

}
class Counter{
int i=1;
}
上面的不知道你能看懂吗??

用正则表达式
int n=0;
String duanluo=段落;
String regex="\\s匹配的单词\\s";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(duanluo);
while(matcher.find()) {
n++;
}
System.out.println(regex+"出现"n+"次");

。。。可能我理解错了 没试 但是这样的思路 会重复输出
String[] aa=duanluo.split(" ");
for(int i=0;i<aa.length;i++){
int n=1;
for(int j=i+1;j<aa.length;j++){
if(aa[i].trim().equals(aa[j].trim())){
n++;
}
}
System.out.println(aa[i]+"出现"+n+"次");
}

java程序:统计一段英文段落中每个单词出现的次数,这个段落存储在一个...
答:param word / public void insert( String word ) { if ( dictionary.containsKey( word ) ) { int currentCount = dictionary.get( word );dictionary.put( word, currentCount + 1 );} else { dictionary.put( word, 1 );} wordsCount++;} / 取得字典里所有不同的单词 return / public ...

用java怎么写出算一段英文单词总数和每个单词出现的次数?
答:import java.util.*;import java.util.regex.*;public class Yugi{ public static void main(String[] args){ String words = "Look buddy, U got work hard and put yourself in your java, Once you learned the heart of the java, I can guarantee that you win."; String reg...

用Java写、题目:输入一行字符,分别统计出其中英文字母、空格、数字和...
答:import java.io.IOException;import java.io.InputStreamReader;public class zifu { public static void main(String[] args) { System.out.println("请输入:(结束请按回车)");InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br= new BufferedReader(isr);String s = ""...

用java 编写一个程序,接受用户输入的一段英文文字,统计出其中的字符个...
答:public static void main(String[] args){ System.out.println("请输入英语片段,以';'结束:");Scanner scanner = new Scanner(System.in);String str = "";int dc = 0;int zc = 0;int jc = 0;while(scanner.hasNext()){ str = scanner.next();zc += str.length();dc++;System.ou...

使用java写一个方法实现统计一条英文语句忠每个单词的个数
答:import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import javax.swing.JOptionPane;public class Test91 { public static void main(String[] args) { String s = JOptionPane.showInputDialog(null, "请输入句子:");String[] ss = s.trim().split...

java解决输入一段字符串,统计其中有多少个单词。(单词用空格隔开...
答:回答:package danci; import java.util.Scanner; public class danci { public static void main(String[] args) { int words=0; System.out.println("请输入字符串:"); Scanner sca =new Scanner

求一个JAVA小程序 要求统计输出的一段英文段落中单词出现的频率_百度知 ...
答: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 ?

java从键盘或文件读取一段英文文字,统计其中单词个数,并输出所有单词...
答:public void test(){java.util.Scanner sc = new java.util.Scanner(System.in);System.out.println("请输入字符串...");String str = sc.next();char[] arr = str.toCharArray();java.util.Map<String, Integer> map = new java.util.HashMap<String, Integer>();for(int i = 0; i...

用JAVA程序,编程一个英文段落中各字母(不分大小写)出现的次数并输出统...
答:import java.util.*;public class CharNumber { public void test(){ Scanner scan=new Scanner(System.in);System.out.println("input the string:");String s=scan.nextLine();char[] ch=s.toCharArray();double[] percent=new double[ch.length];//存放比例 char[] resultch=new char[ch....

用java怎么实现统计一英文文档里各个英语字母的个数及所占百分比_百度...
答:import java.io.IOException;import java.text.DecimalFormat;import java.text.NumberFormat;public class Test { private static NumberFormat nf = new DecimalFormat("0.00");public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("c:/...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网