【笔试】7、统计出其中英文字母、空格、数字和其它字符的个数

来源:互联网 发布:工具书阅读软件 编辑:程序博客网 时间:2024/06/07 06:24
/** * 题目:题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。    * 时间:2015年7月28日10:04:33 * 文件:lianxi07.java * 作者:cutter_point */package bishi.zuixin50.t2015728;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;public class Lianxi07 {public static void main(String[] args){//统计所有的英文字母,也就是ascii码String path = "source/zuixin50/lianxi07input.txt";String path2 = "source/zuixin50/lianxi07output.txt";List lis = Lianxi07.getLines(path);//获取文本中所有的字符Map map = Lianxi07.count(lis);//吧统计的数据输出到文件中Lianxi07.out(map, path2);}public static void out(Map map, String path){//我们遍历map,吧数据输出到文件中FileOutputStream fos = null;try {fos = new FileOutputStream(new File(path));//遍历mapfor(Object o : map.entrySet()){Map.Entry<String, Integer> entry = (Entry<String, Integer>) o;//其实这里可以直接输出:out = entry.getKey() + "的个数是:" + entry.getValue() + "\n";//这样写就不用进行判断了if(entry.getKey() == "zimu"){String out = "字母的个数是:" + entry.getValue() + "\n";System.out.println(out);fos.write(out.getBytes());}else if(entry.getKey() == "kongge"){String out = "空格的个数是:" + entry.getValue() + "\n";System.out.println(out);fos.write(out.getBytes());}else if(entry.getKey() == "shuzi"){String out = "数字的个数是:" + entry.getValue() + "\n";System.out.println(out);fos.write(out.getBytes());}else{String out = "其他的个数是:" + entry.getValue() + "\n";System.out.println(out);fos.write(out.getBytes());}}} catch (Exception e) {e.printStackTrace();}finally{try {fos.close();} catch (Exception e2) {e2.printStackTrace();}}}/** * 我们统计这一行数据中的所有的字母,空格、数字和其他字符 * @param lines 一行字符串 * @return */public static Map count(List lines){//这里我们用一个map来存放相应的数据,key是我们要统计的属性:字母,空格。。。value是值个数Map tongji = new HashMap<String, Integer>();//初始化maptongji.put("zimu", 0);tongji.put("kongge", 0);tongji.put("shuzi", 0);tongji.put("other", 0);//其他//首先我们遍历所有的行Iterator it = lines.iterator();while(it.hasNext()){//一行一行的取值char[] all = ((String) it.next()).toCharArray();//我们遍历这个字符数组for(int i = 0; i < all.length; ++i){if((all[i] >= 'a' && all[i] <= 'z') || (all[i] >= 'A' && all[i] <= 'Z')){int value = (int) tongji.get("zimu");//我们把数据+1tongji.put("zimu", ++value);}else if((all[i] >= '0' && all[i] <= '9')){//我们统计数字的个数int value = (int) tongji.get("shuzi");//我们把数据+1tongji.put("shuzi", ++value);}else if(all[i] == ' '){//如果是空格的话int value = (int) tongji.get("kongge");//我们把数据+1tongji.put("kongge", ++value);}else{//其他int value = (int) tongji.get("other");//我们把数据+1tongji.put("other", ++value);}}//for}//while//统计完毕return tongji;}public static String getLine(String path){//从文件中获取一行数据String line = null;//一个文件输入流InputStreamReader isr = null;//用来读取一行BufferedReader br = null;try {isr = new InputStreamReader(new FileInputStream(new File(path)));br = new BufferedReader(isr);line = br.readLine();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//关闭IO资源try {br.close();isr.close();} catch (Exception e) {e.printStackTrace();}}return line;}//读取文件的内容保存到String的数组中public static List getLines(String path){//从文件中获取一行数据String line = null;List<String> lines = new ArrayList();//一个文件输入流InputStreamReader isr = null;//用来读取一行BufferedReader br = null;try {isr = new InputStreamReader(new FileInputStream(new File(path)));br = new BufferedReader(isr);while( (line = br.readLine()) != null){//吧读取到的一行数据存放到数组中lines.add(line);}} catch (Exception e) {e.printStackTrace();}finally{try {//关闭IO资源br.close();isr.close();} catch (Exception e) {e.printStackTrace();}}return lines;}}

0 0