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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

题目需求如下:
5、 统计一个文本文件中字符出现的次数,结果存入另外的一个文本文件中。例如:
    a:  21 次
    b:  15 次
    c: 15 次

我知道用TreeMap集合可以解决,不过我一开始没用这个。。。然后就卡住了。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;


public class Test{
        public static void main(String[] args) throws Exception{
                File f1 = new File("d:\\","ceshi.txt");   //D盘根目录下的 ceshi.txt文件

                        //"aaabbbddddcccewewe"    这是文件中的字符串
       
                BufferedReader br = new BufferedReader(new FileReader(f1));//整了个缓冲区,把数据读出来放到了数组中
                        byte[] b = new byte[1024];
                        b=br.readLine().getBytes();
                        int count =1;   //计数器
                for(int x=0 ;x<b.length-1;x++){
                       
                        for(int y=x+1; y<b.length;y++){
                                if(b[x]==b[y]){
                                        count++;         //嵌套FOR循环,来记字符出现的次数
                                }
                        }
                       
                                System.out.println((char)b[x]+"出现的次数为"+count);
                                count=1;
                }
        }
}


输出结果如下:
a出现的次数为3
a出现的次数为2
a出现的次数为1
b出现的次数为3
b出现的次数为2
b出现的次数为1
d出现的次数为4
d出现的次数为3
d出现的次数为2
d出现的次数为1
c出现的次数为3
c出现的次数为2
c出现的次数为1
e出现的次数为3
w出现的次数为2
e出现的次数为2
w出现的次数为1


问题就在这里了,我想每个字符只匹配一次,这样就可以解决需求了,不过没想出来。各位大神,看看有啥办法么?

4 个回复

倒序浏览
还没有学到那呢!过来先预习下!
回复 使用道具 举报
学习中,坐等答案
回复 使用道具 举报
暂时只能想到这个方法了。。。。。你参考下
package luntan;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class TestDemo {

        /**
         * @param args
         * @throws IOException
         */
        /*
         * 需求: 统计一个文本文件中字符出现的次数,结果存入另外的一个文本文件中 1.统计字符出现的次数,这个可以根据之前学的map集合
         * 2.将统计次数根据io流存储到另一个文本中
         */
        public static void main(String[] args) throws IOException {
                // 将文本文件封装
                File file = new File("f:\\test.txt");
                // 读取文本文件
                String str = read(file);
                String s = getCharCount(str);
                // 将计数好的字符存储到另一个文件
                cunFile(s);

        }

        public static void cunFile(String str) throws IOException {
                File dir = new File("f:\\");
                ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
                FileOutputStream fos = new FileOutputStream(new File(dir, "test2.txt"));
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = bis.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                }
                fos.close();
                bis.close();
        }

        public static String getCharCount(String str) {
                // 字符串转换成字符数组
                char[] chs = str.toCharArray();
                // 定义map集合
                Map<Character, Integer> map = new TreeMap<Character, Integer>();
                for (int i = 0; i < chs.length; i++) {
                        // 排除字符串出现其他的字符
                        if (chs[i] >= 'a' && chs[i] <= 'z' || chs[i] >= 'A'
                                        && chs[i] <= 'Z') {
                                // 将字符转转成的字符数组,作为key,获取value
                                Integer value = map.get(chs[i]);
                                int count = 1;
                                if (value != null) {
                                        count = value + 1;
                                }
                                // 替换后的key和value
                                map.put(chs[i], count);
                        }
                }

                return mapToString(map);

        }

        public static String mapToString(Map<Character, Integer> map) {

                StringBuilder sb = new StringBuilder();

                Iterator<Character> it = map.keySet().iterator();

                while (it.hasNext()) {
                        Character key = it.next();
                        Integer value = map.get(key);

                        sb.append(key + ":" + value + "\r\n");
                }

                return sb.toString();
        }

        public static String read(File file) throws IOException {
                FileReader fr = new FileReader(file);
                char[] buf = new char[1024];
                int len = fr.read(buf);
                String str = new String(buf, 0, len);
                return str;
        }

}


回复 使用道具 举报
写的有点粗糙,你看看可行。。。。没仔细备注
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马