统计字符串中各个字符的数量(提示:字符不用排序)

原创 2018年04月15日 10:17:33
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

/*分析以下需求,并用代码实现	
1.利用键盘录入,输入一个字符串
2.统计该字符串中各个字符的数量(提示:字符不用排序)
3.如:
	用户输入字符串"If~you-want~to~change-your_fate_I_think~you~must~come-to-the-dark-horse-to-learn-java"
	程序输出结果:-(9)I(2)_(3)a(7)c(2)d(1)e(6)f(2)g(1)h(4)i(1)j(1)k(2)l(1)m(2)n(4)o(8)r(4)s(2)t(8)u(4)v(1)w(1)y(3)~(6)*/
public class t6 {
	public static void main(String[] args) {
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个字符串:");
		//接收字符串
		String s = sc.nextLine();
		
		//将字符串转换成数组
		char[] ch = s.toCharArray();
		
		//创建Map集合
		HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
		
		//遍历字符数组
		for (int i = 0; i < ch.length; i++) {
			
			//将数组中的字符作为键去查询map表
			Integer value = hm.get(ch[i]);
			
			//判断值是否为null
			int count = 0;
			if( value != null) {
				count = value;
			}
			count++;
			hm.put(ch[i], count);
		}
		
		//遍历集合
		Set<Entry<Character, Integer>> entrys = hm.entrySet();
		for (Entry<Character, Integer> entry : entrys) {
			Character key = entry.getKey();
			Integer value = entry.getValue();
			System.out.print(key+"("+value+")");
		}
	}
}

ArrayList和HashMap的结合使用

2.分析以下需求,并用代码实现:  (1)利用键盘录入,输入一个字符串  (2)统计该字符串中各个字符的数量  (3)如:   用户输入字符串"If~you-want~to~change-yo...
  • Angel_guoo
  • Angel_guoo
  • 2018-01-13 23:26:36
  • 195

统计一组字符串中各个单词出现的次数

用代码实现以下需求 (1)有如下字符串"If you want to change your fate I think you  must come to the dark horse to le...
  • Angel_guoo
  • Angel_guoo
  • 2018-01-22 21:23:21
  • 187

TortoiseSVN 日常操作指南

TortoiseSVNA Subversion client for WindowsStefan KüngLübbe OnkenSimon Large2005/01/17 19:09:21二〇〇五年四...
  • happy4nothing
  • happy4nothing
  • 2005-05-19 22:49:00
  • 99044

android 4.3源码下载【自己下载成功的步骤、其中遇到很多你可能会遇到的很多问题】

1、先是系统查看是32位的还是64位 jianguoliao@jianguoliao-Lenovo-IdeaPad-Y470:~$ getconf LONG_BIT 32 jianguoliao@ji...
  • jianguo_liao19840726
  • jianguo_liao19840726
  • 2013-08-05 23:08:17
  • 18607

hdu2159FATE【二维完全背包】

Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。...
  • zhou_yujia
  • zhou_yujia
  • 2015-10-27 21:03:53
  • 544

xcode 7 错误:CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG

Oct 12 16:01:45 yingyong[1639] : CGContextSaveGState: invalid context 0x0. If you want to see the ba...
  • u010828718
  • u010828718
  • 2015-10-12 16:15:30
  • 2278

If you want to happiness

If you want to happiness, the heart is a little bit more simple; If you want to free and easy point,...
  • u014488563
  • u014488563
  • 2014-04-01 15:55:39
  • 130

iOS报错 - If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variabl

Error: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CON...
  • iOSTianNan
  • iOSTianNan
  • 2016-04-06 13:23:58
  • 6729

OCP-1Z0-052-V8.02-93题

93. You are using flat files as the data source for one of your data warehousing applications. To op...
  • rlhua
  • rlhua
  • 2013-11-18 09:26:23
  • 9746

HDU-2159 FATE(二维费用背包问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 题目: FATE Time Limit: 2000/1000 MS (Java/Others...
  • qq_33171970
  • qq_33171970
  • 2016-01-26 17:17:46
  • 628
收藏助手
不良信息举报
您举报文章:统计字符串中各个字符的数量(提示:字符不用排序)
举报原因:
原因补充:

(最多只允许输入30个字)