获取一段字符串中每个单词的次数(用空格分隔)
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+"次"); } } }
相关文章推荐
- 获取一段字符串 每个单词的次数
- 每天一道LeetCode-----以单词为单位逆序字符串,每个单词之间以一个空格分隔(原字符串中可能有多个空格)
- 计算出一字符串(字符串中每个单词之间有一个或多个空格)中每个单词的 出现的次数
- java编程题:输入一段英文,获取每个单词出现的次数跟位置,并排序
- 编写函数,对标准输入进行扫描,并对“the”出现的次数进行计数。 进行比较时应区分大小写。认为一个单词有一个或多个空格字符分隔, 并且输入行在长度上不会超过100个字符,计数结果输出。
- 将字符串中的每个单词顺序进行颠倒,单词还是原来的单词,字母顺序不发生变化,将字符串中的空格用 代替
- 编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词(只使用循环,数组)
- 获取字符串中每个字母出现的次数
- 编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。
- 不使用for完成一段有空格间隔的字符串,分辨长度大于等于4的单词(求各位高人修改,我表示我是菜鸟,这个算法实在太长了)
- 获取字符串中的数字、符号、中文、英文单词、字母、空格、字节、其他字符的个数
- 编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空 格,用空格来分隔单词。
- C语言 已知的字符串中找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词。
- 编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。
- C语言编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。
- 编程在一个已知的字符串中找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词。
- String去除字符串两端空格、将一个字符串进行反转、计算字符串中出现的的次数、获取两个字符串中最大相同字串
- 从控制台获取一串字符串,并对每个字符出现的次数进行统计
- 输入一行字符串,统计其中有多少个单词,单词之间用一个或者多个空格分隔开
- 编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。