java实现统计文章(英文)中字母、数字、空格和其他字符出现的次数

import java.util.Scanner;

/**
 *
 * @author Administrator 统计字符串中字母数字和空格等字符出现的个数。
 *
 */
public class CalculateCharacter {
 public static void calculate(char[] date) {
  int zimu = 0, shuzi = 0, kongge = 0, qita = 0;
  for (char i : date) {
   if (i >= 65 && i <= 90 || (i >= 97 && i <= 122)) {
    zimu++;
   } else if (i == 32) {
    kongge++;
   } else if (i >= 48 && i <= 57) {
    shuzi++;
   } else {
    qita++;
   }
  }
  System.out.println("字母" + zimu + "数字" + shuzi + "空格" + kongge + "其它"
    + qita);
 }

 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  String in = scanner.next();
  char[] date = in.toCharArray();
  calculate("guogenfang 112358 @@@ hello".toCharArray());
 }
}


import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author guogenfang 
 * calculate the file's word appear probability;
 *
 */
public class fileReader {
 public static void main(String[] args) throws Exception {
  File file = new File("D:\\1.txt");
  BufferedReader read = new BufferedReader(new FileReader(file));
  String word = read.readLine();
  List<String> character = new ArrayList<String>();
  while (word != null) {
   System.out.println(word);
   character.add(word);
   word = read.readLine();
  }
  CalculateCharacter.calculate(character.toString().toCharArray());
 }
}
 

你可能感兴趣的:(java)