您的位置:首页 > 编程语言 > Java开发

获取一段字符串中每个单词的次数(用空格分隔)

2016-10-28 16:08 316 查看
package com.company;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
* Created by ttc on 16-10-28.
*/
public class ShowWordsTimes
{
public static void main(String[] args)
{
System.out.println("请输入一段英文");
Scanner sc=new Scanner(System.in);
String strPara=sc.nextLine();
String[] strArray=strPara.split(" ");
Map<String,Integer> mapWords=new HashMap<String,Integer>();
for(String str : strArray)
{
if(!mapWords.containsKey(str))
{
mapWords.put(str,1);
}
else
{
Integer iCount =mapWords.get(str);
iCount++;
mapWords.put(str,iCount);
}
}
for(Map.Entry<String,Integer> me: mapWords.entrySet())
{
String strKey=me.getKey();
Integer iCount = me.getValue();
System.out.println(strKey+"出现了"+iCount+"次");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java map
相关文章推荐