A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wudigod12 中级黑马   /  2015-7-23 21:37  /  391 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看到别人帖子里的一道面试题, 挺有趣的, 自己写出来和大家交流下.

D盘中有一文件a.txt,  内容有字母和数字, 用户输入密码"heima"可读取文件内容,  统计出每个字母出现的次数,按照"a:1个 b:2个"的格式打印出来.

package test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class test6 {
        public static void main(String[] args) throws IOException{
                //获取用户输入的密码
                Scanner scan = new Scanner(System.in);
                String str = null;
               
                while((str = scan.nextLine()) != null){
                        //密码正确则读取文件
                        if("heima".equals(str)){
                                readFile();
                                break;
                        }else{
                                System.out.println("请重新输入密码");
                        }
                }
        }
       
        public static void readFile() throws IOException{
                TreeMap<Character,Integer> m = new TreeMap<Character,Integer>();
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\a.txt"));
                int by = 0;
                while((by=bis.read()) != -1){
                        //判断是否为字母,排除数字和符号
                        if((by >= 'a' && by <='z') || (by >= 'A' && by <= 'Z')){
                                //System.out.print((char)by);
                                //如果Map中没有这个键,则初始化这个键的值为1
                                if(!m.containsKey((char)by)){
                                        m.put((char)by, 1);
                                }else{
                                        //如果存在,则值加1
                                        m.put((char)by, m.get((char)by) + 1);
                                }
                        }
                }
                //按格式输出统计数据.
                Set<Entry<Character, Integer>> set = m.entrySet();
                Iterator<Entry<Character, Integer>> it = set.iterator();
                while(it.hasNext()){
                        Entry<Character,Integer> e = it.next();
                        System.out.print(e.getKey() + ":" + e.getValue() + "个\t");
                }
        }
}



10 个回复

倒序浏览
思路很巧妙 !
回复 使用道具 举报
现在看起还是有点难度
回复 使用道具 举报
我这里也有一个类似的   自己写的
  1. package com.itheima;
  2. import java.io.*;
  3. import java.util.*;
  4. /*7、 统计一个文本文件中字符出现的次数,结果存入另外的一个文本文件中。例如:
  5.    a:  21 次
  6.    b:  15 次
  7.    c: 15 次
  8.         把:  7 次
  9.         当:  9 次
  10.         前:  3 次
  11.         ,:30 次*/
  12. public class Test7 {

  13.         private static Integer value;

  14.         public static void main(String[] args) {
  15.                 BufferedReader br = null;
  16.                 FileWriter fw = null;
  17.                 try {//关联文件
  18.                         br = new BufferedReader(new FileReader("e:\\a.txt"));
  19.                         fw = new FileWriter("e:\\b.txt");
  20.                         //字符与次数以键值对的形式存放到集合中,使用TreeMap集合
  21.                         TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();
  22.                         value = null;
  23.                         int count = 0;
  24.                         String str = null;
  25.                         //每次读取一行
  26.                         while((str = br.readLine())!=null)
  27.                         {       
  28.                                 //System.out.println(str);
  29.                                 for(int i=0;i<str.length();i++)
  30.                                 {//此过程类似于统计字符串中字母出现次数了,不再赘述
  31.                                         value = hm.get(str.charAt(i));
  32.                                         if(value != null)
  33.                                                 count = value;
  34.                                         count++;
  35.                                         hm.put(str.charAt(i), count);
  36.                                         count = 0;
  37.                                 }
  38.                                 //System.out.println(hm.size());
  39.                                
  40.                         }
  41.                         //System.out.println(hm.size());
  42.                         StringBuilder sb = new StringBuilder();//存放特定格式数据
  43.                         //定义set集合存放键值对映射关系,可以迭代
  44.                         Set<Map.Entry<Character, Integer>> entrySet = hm.entrySet();
  45.                         Iterator<Map.Entry<Character, Integer>>it = entrySet.iterator();
  46.                         while(it.hasNext())
  47.                         {//获取每一对映射关系,取出相应的键值对。
  48.                                 Map.Entry<Character, Integer>me = it.next();
  49.                                 Character chs = me.getKey();//获取键和值
  50.                                 Integer counts = me.getValue();
  51.                                 sb.append(chs+":    "+counts+"次   "+"\r\n");//存储                               
  52.                         }
  53.                         fw.write(sb.toString()+"\r\n");//将容器中的数据写入到文件
  54.                         fw.flush();//刷新流中的数据到文件
  55.                         System.out.println(sb.toString()+"\r\n");
  56.                                
  57.                 } catch (Exception e) {
  58.                         // TODO: handle exception
  59.                         e.getStackTrace();
  60.                 }
  61.         }

  62. }
复制代码
回复 使用道具 举报
明天做一下哈哈
回复 使用道具 举报
值得一看啊 ,这种面试题可以多发点
回复 使用道具 举报
这个题综合性好强!
回复 使用道具 举报
值得看看
回复 使用道具 举报
xiaoxinxin003 发表于 2015-7-23 23:37
我这里也有一个类似的   自己写的

:lol我们想到一起去了. 以后多多交流~
回复 使用道具 举报
谢谢分享                                
回复 使用道具 举报
得好好的巩固所学的知识才能做出来呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马