T001_UT001_0018

来源:互联网 发布:foxmail公司邮箱数据 编辑:程序博客网 时间:2024/06/10 02:49

编写一个程序,从标准输入设备上输入若干行英文语句,直到输入的一行字符串等于"finished input"。回车后停止输入,并对所输入的所有语句进行分析,统计除逗号,句号,感叹号和问号以外的所有单词的频次并打印在标准输出设备上。输出顺序按Java语言的字符串的自然顺序排序。

字符串自然顺序是指,对字符串从左到右,每个字符按照其字符的ascii码从小到大排序,如果第1个字符相同,则比较第二个字符,然后以此类推。

 

 

举例一:

输入:

1
2
3
4
5
6
This is a very simple problem. I can solve this in a minute! I don't think this is a simple problem....
Ok, I finished this one.
I think I can do more.
That's good!
I hope I can write a robot in future.
finished input
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
I,7
Ok,1
That's,1
This,1
a,4
can,3
do,1
don't,1
finished,1
future,1
good,1
hope,1
in,2
is,2
minute,1
more,1
one,1
problem,2
robot,1
simple,2
solve,1
think,2
this,3
very,1
write,1

import java.util.*;public class T001_UT001_0018{public static void main(String[] args){Scanner njp_input=new Scanner(System.in);String[] njp_str=new String[10000];String njp_Str="";int njp_count=0;while(true){njp_str[njp_count]=njp_input.nextLine();if(njp_str[njp_count].equals("finished input"))break;else{njp_Str+=njp_str[njp_count];njp_count++;}}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();    njp_count=0;     while(njp_iterator.hasNext()){        String word = (String)njp_iterator.next();        if(njp_count!=0)        njp_StringBuffer.append(word).append(",").append(njp_TreeMap.get(word)).append("\n");        njp_count++;    }System.out.print(njp_StringBuffer.toString());}}


0 0