`

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

 
阅读更多
Java代码   收藏代码
  1. public static void main(String[] args)  
  2. {  
  3.     BufferedReader bfr = new BufferedReader(new FileReader("d:\1.txt"));  
  4.     StringBuffer sbf = new StringBuffer();  
  5.     String line;  
  6.     int character = 0,digital = 0, blank = 0, other = 0;  
  7.     while((line = bfr.readLine()) != null)  
  8.     {  
  9.          sbf.append(line+"\r\n");  
  10.     }  
  11.     String str = sbf.toString();  
  12.     char[] arr = str.toCharArray();  
  13.     for(char c : arr)  
  14.     {  
  15.         if((c >= 'a' && a <= 'z') || (c >= 'A' && c <= 'Z'))  
  16.         {  
  17.             character++;  
  18.         }  
  19.         else if(c >= '0' || c <= '9')  
  20.         {  
  21.             digital = 0;  
  22.         }  
  23.         else if(c == ' ')  
  24.         {  
  25.             blank++;  
  26.         }  
  27.         else  
  28.         {  
  29.             other++;  
  30.         }  
  31.         System.out.println("输入的符串为:"+str);   
  32.         System.out.println("总共字符数目为"+str.length());   
  33.         System.out.println("字母数目"+character);   
  34.         System.out.println("数字数目"+digital);   
  35.         System.out.println("空格数目"+blank);  
  36.         System.out.println("其他字符数目"+other);  
  37.     }  
  38. }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics