编写一个程序,从标准输入设备上输入一行英文语句。敲击回车后对该语句进行分析,统计除逗号,句号,感叹号和问号以外的所有单词的频次并打印在标准输出设备上。输出顺序按Java语言的字符串的自然顺序排序。
字符串自然顺序是指,对字符串从左到右,每个字符按照其字符的ascii码从小到大排序,如果第1个字符相同,则比较第二个字符,然后以此类推。
举例一:
输入:
1
|
This
is a very simple problem. I can solve this in a
minute! I don't think this is a simple problem.... |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
I,2 This,1 a,3 can,1 don't,1 in ,1 is,2 minute,1 problem,2 simple,2 solve,1 think,1 this,2 very,1 |
import java.util.*;
public class T001_UT001_0017
{
public static void main(String[] args)
{
Scanner njp_input=new Scanner(System.in);
String njp_str=njp_input.nextLine();
StringBuffer njp_StringBuffer = new StringBuffer();
TreeMap<String ,Integer> njp_TreeMap = new TreeMap<String ,Integer> ();
String[] njp_str_list = njp_str.split(" |,|\\?|\\.|\\!|\\....");
for (int i = 0; i < njp_str_list.length; i++)
{
if (!njp_TreeMap.containsKey(njp_str_list[i]))
njp_TreeMap.put(njp_str_list[i], 1);
else
njp_TreeMap.put(njp_str_list[i],njp_TreeMap.get(njp_str_list[i])+1 );
}
Iterator<String> njp_iterator = njp_TreeMap.keySet().iterator();
int count=0;
while(njp_iterator.hasNext())
{
String word = (String)njp_iterator.next();
if(count!=0)
njp_StringBuffer.append(word).append(",").append(njp_TreeMap.get(word)).append("\n");
count++;
}
System.out.print(njp_StringBuffer.toString());
}
}