探索软件工程道路上的我III (Θ∀Θ#)

Posted on 2016-03-29 22:16  陈俊宇JUN  阅读(289)  评论(0编辑  收藏  举报

github地址https://github.com/JUNYU217/2016-03-08

开发语言Java

开发工具UltraEdit

|| 为月末了,网费欠了...很抱歉的拖了那么久的作业   QAQ  ||

------个人小结------

通过这次程序的编写,我把java自学了一遍(原先只会用c++..)通过百度了解到了map的使用方法 ,差强人意,不过总归是编出来了。

 ------作业要求------

  (1). 实现一个控制台程序,给定一段英文字符串,统计其中各个英文单词(4字符以上含4字符)的出现频率。

    • 字母: A-Z, a-z.
    • 字母数字: A-Z, a-z, 0-9.
    • 分隔符: 字母数字
    • 单词:
    • 包含有4个4个以上的字母
    • 单词分隔符分开
    • 如果一个字符串包含_非_字母数字,则不是单词
    • 单词大小写不敏感,例如 “file”、“FILE”和“File”可以看作同一个单词
    • 单词必须是字母开头,“file123”是单词,“123file”不是单词

(2). 示例

输入

Word is case insensitive, i.e. “file”, “FILE” and “File” are considered the same word.

输出

file: 3

word: 2

case: 1

considered: 1

insensitive: 1

same: 1

 ------程序编写------

 

先将测试的文段改造一下,并将其分割开

  text = text.replaceAll("[\\‘‘.“”,,]", "");  //删除字符串中的标点符号
  text = text.toLowerCase();//将文字全部转换为小写
  StringTokenizer take =  new StringTokenizer(text); //StringTokenizer是一个用来分隔String的应用类

将分割开的单词存入到map中

 

while( take.hasMoreElements() )
{
            
    word=take.nextToken() ;
        if(word.length()>=4)
    {
              Integer count = map.get(word);
          if(count == null)
          {
              map.put(word,1);
          }
          else
          {
              map.put(word,++count);
          }
        }
    i++;
}
View Code

通过遍历打印出结果

 

for(Map.Entry<String,Integer> entry : map.entrySet())
{  
    System.out.println(entry.getKey() +" : "+ entry.getValue());
}
View Code

 

------程序运行------

 

测试数据:Word is case insensitive, i.e. “file”, “FILE” and “File” are considered the same word.

测试结果: