严瑞

导航

 

第三次作业对我来说难度太大了,java刚学并不太会,借鉴了同学的代码测试运行。

源代码:

package ff;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class F { public static void main(String arg[])
{ Map<String,Integer> map=new HashMap<String,Integer>();
String content="Word is case insensitive, i.e. “file”, “FILE” and “File” are considered the same word."+
"If you weeped for the missing sunset,you would miss all the shining stars";
content=content.toLowerCase();
StringTokenizer token=new StringTokenizer(content);
while(token.hasMoreTokens())
{
String word=token.nextToken(", “”?.!:\"\"''\n");
int count;
if(map.containsKey(word))
{
count=map.get(word);
map.put(word, count+1);
}
else
map.put(word, 1);
}
sort(map);
}

public static void sort(Map<String,Integer> map)
{
List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
for (int i = 0; i < infoIds.size(); i++)
{
Entry<String, Integer> id = infoIds.get(i);
if(id.getKey().length()>=4)
{System.out.println(id.getKey()+":"+id.getValue());}
}
}

}

运行结果如下:

posted on 2016-03-16 12:37  严瑞  阅读(203)  评论(2编辑  收藏  举报