查询字符串中字母,数字,空格出现的次数

本文介绍了一个使用Java实现的简单程序,该程序能够读取指定路径下的文本文件,并统计文件中字母、数字、空格及其他字符的数量。通过逐行读取文件并遍历字符串中的每个字符来完成统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void main(String[] args)
{
	BufferedReader bfr = new BufferedReader(new FileReader("d:\1.txt"));
	StringBuffer sbf = new StringBuffer();
	String line;
	int character = 0,digital = 0, blank = 0, other = 0;
	while((line = bfr.readLine()) != null)
	{
		 sbf.append(line+"\r\n");
	}
	String str = sbf.toString();
	char[] arr = str.toCharArray();
	for(char c : arr)
	{
		if((c >= 'a' && a <= 'z') || (c >= 'A' && c <= 'Z'))
		{
			character++;
		}
		else if(c >= '0' || c <= '9')
		{
			digital = 0;
		}
		else if(c == ' ')
		{
			blank++;
		}
		else
		{
			other++;
		}
		System.out.println("输入的符串为:"+str); 
		System.out.println("总共字符数目为"+str.length()); 
		System.out.println("字母数目"+character); 
		System.out.println("数字数目"+digital); 
		System.out.println("空格数目"+blank);
		System.out.println("其他字符数目"+other);
	}
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值