`
f51889920
  • 浏览: 38207 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

 
阅读更多
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);
	}
} 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics