Java编程:输入一段英文:1、统计一共有多少个单词;2、每个单词出现的次数;3、按出现次数升或降序排列

求大神帮忙呀,,很急,,,如果不想公开的话,就发我邮箱吧956389598@qq.com
感谢ing………………

自己去调整,随便写的;

package com.baidu.com.java;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MatchWorld {
public static void main(String[] args) {
String str = "Is there anyone who hasn't suffered for the secret love? We alwaysthink that"
+ " love is very heavy, heavy and could be the heaviest thing inthe world. "
+ "But one day, when you look back, you suddenly realize thatit's always light, "
+ "light. We all thought love was very deep, but infact it's very thin. " + "The deepest and heaviest love must grow up withthe time";
// 全部转换成大写
str = str.toUpperCase();
//或许你可以考虑用空格分割
String[] rang = str.split("\\b");
// 自己去调整吧,如果要得到精确的
System.out.println("单词总数大概为:" + rang.length);

// 用来记录的单词
Map<String, Integer> map = new HashMap<String, Integer>();
int countSpace = 0;

//统计各个字符出现的次数
for (String s : rang) {
if (s.trim().length() > 0) {
s = s.trim();
if (!map.containsKey(s)) {
int count = str.split("\\b" + s.trim() + "\\b").length;
map.put(s, count);
}
} else {
map.put("空格", countSpace++);
}
}

//单词出现次数
for (String key : map.keySet()) {
System.out.println(key + "出现:" + map.get(key) + "次");
}

//把元素添加到list
List<String> list = new ArrayList<String>();
list.addAll(map.keySet());

// 排序前
System.out.println("\n排序前:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}

// 开始排序
System.out.println("\n按照出现次数降序排列(AESC):\n");
for (int i = 1; i < list.size(); i++) {
for (int j = 0; j < list.size() - i; j++) {
if (map.get(list.get(j)) > map.get(list.get(j+1))) {
String tmp = list.get(j);
list.set(j, list.get(j+1));
list.set(j + 1, tmp);
}
}
}

// 排序后
System.out.println("\n排序后:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-10-27
这个很容易啊!根据空格来判断单词数用
String[] result = "this is a test".split(" ");
方法来将这句英文按单词赋值到数组!然后对数组操作就可以了!

Java编程:输入一段英文:1、统计一共有多少个单词;2、每个单词出现的次数...
Java编程:输入一段英文:1、统计一共有多少个单词;2、每个单词出现的次数;3、按出现次数升或降序排列 15 求大神帮忙呀,,很急,,,如果不想公开的话,就发我邮箱吧956389598@qq.com感谢ing………乐乐堂的宝贝 | 浏览2201 次 |举报 我有更好的答案邀请更新 2012-10-26 最佳答案 自己去调整,随便写的; p...

用java 编写一个程序,接受用户输入的一段英文文字,统计出其中的字符个...
System.out.println("单词个数为:" + dc);System.out.println("字母数为:" + (zc - 1));System.out.println("句数为:" + jc);}

用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编程:输入一行英文,判断中间有多少个单词,单词间以空格隔开,连续出 ...
Java程序:public class Main {public static void main(String[] args){String text = "How do you do!";System.out.println(getWords(text));}public static int getWords(String text) {int count = 0;int len = text.length();int i, j;i = 0;while(true) {for(; i<len && tex...

Java 统计单词个数并按照出现次数进行排序
public static void main(String[] args) { \/\/ TODO Auto-generated method stub String str = "I am a student";String[] arr = str.split(" ");System.out.println("共"+arr.length+"个单词");for(String item:arr) System.out.println(item);} 如果对您有帮助,请记得采纳为满意答案...

...单词,相邻的两个单词用空格隔开,编程统计单词的个数。
public static void main(String[] args) { \/\/ TODO Auto-generated method stub int LogOut = 1;Scanner input = new Scanner(System.in);System.out.println("欢迎使用XX登录系统");do{ System.out.print("请输入用户名:");String userName = input.next();System.out.print("请输入用户...

Java编程!键盘输入一段英文语句,将这段话写入文件file.txt中,然后统...
import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.util.Scanner;public class Test2 { public static void main(String[] args) throws Exception{ Scanner san = new Scanner(System.in);System.out.println("Input the text:");String strText = san....

...用户输入某个单词,求该单词出现的总次数,和出现在第几个位置上_百度...
+ 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);}} ...

java程序:统计一段英文段落中每个单词出现的次数,这个段落存储在一个...
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...

用java写一个统计字符串中指定单词的个数
bookabcbookadbook";str=" "+str+" ";单词是s="book";直接输出str.split(s).length-1就是单词的个数。原理就是字符串前后都加空格以后,再用你要查找的单词把字符串分成数组,数组元素的个数减1,就是单词的个数了。当然,首先要用contains() 方法判断一下字符串是否已经包含要找的单词。

相似回答