计算文件中每个字符的数量

时间:2016-05-11 18:18:57

标签: java char filereader

我通过char读取文本文件char的内容,然后我按升序对它们进行排序并计算每个char出现的次数。当我运行该程序时,我的数字已经过时了,例如有7' A'在文件中,但我得到17.我认为这意味着我的计数出现问题,或者我读取字符的方式。关于什么是错的任何想法?

public class CharacterCounts {

    public static void main(String[] args) throws IOException{
        String fileName = args[0];
        BufferedReader in = new BufferedReader(new FileReader(new File(fileName)));
        ArrayList<Character> vals = new ArrayList<Character>();
        ArrayList<Integer> valCounts = new ArrayList<Integer>();

        while(in.read() != -1){
            vals.add((char)in.read());
        }

        Collections.sort(vals);

        //This counts how many times each char occures,
        //resets count to 0 upon finding a new char.
        int count = 0;
        for(int i = 1; i < vals.size(); i++){
            if(vals.get(i - 1) == vals.get(i)){
                count++;
            } else {
                valCounts.add(count + 1);
                count = 0;
            }
        }

        //Removes duplicates from vals by moving from set then back to ArrayList
        Set<Character> hs = new HashSet<Character>();
        hs.addAll(vals);
        vals.clear();
        vals.addAll(hs);

        //System.out.print(vals.size() + "," + valCounts.size());

        for(int i = 0; i < vals.size(); i++){
            //System.out.println(vals.get(i));
            System.out.printf("'%c' %d\n", vals.get(i), valCounts.get(i));
        }

    }
}

3 个答案:

答案 0 :(得分:2)

写作时

if(vals.get(i - 1) == vals.get(i)){

两者都是完全不同的参考文献,它们完全不相同。你必须比较它们的价值。

你想要

if(vals.get(i - 1).equals(vals.get(i))){

答案 1 :(得分:1)

我认为你的计数逻辑过于复杂。此外,您在循环中调用read()两次,以便跳过其他所有值。

int[] counts = new int[256]; // for each byte value
int i;
while ((i = in.read()) != -1) { // Note you should only be calling read once for each value
    counts[i]++;
}


System.out.println(counts['a']);

答案 2 :(得分:0)

为什么不使用正则表达式,代码将更加灵活和简单。看看下面的代码:

 ...
 final BufferedReader reader = new BufferedReader(new FileReader(filename));
 final StringBuilder contents = new StringBuilder();
 //read content in a string builder
 while(reader.ready()) {
    contents.append(reader.readLine());
 }
 reader.close();

 Map<Character,Integer> report = new TreeMap<>();
 //init a counter    
 int count = 0;
 //Iterate the chars from 'a' to 'z'
 for(char a = 'a';a <'z'; a++ ){
     String c = Character.toString(a);

      //skip not printable char
      if(c.matches("\\W"))
          continue;

      String C = c.toUpperCase();
      //match uppercase and lowercase char
      Pattern pattern = Pattern.compile("[" + c + C +"]", Pattern.MULTILINE);
      Matcher m = pattern.matcher(contents.toString());
         while(m.find()){
             count++;
         }

         if(count>0){
             report.put(a, count); 
         }
         //reset the counter
         count=0;

      }

      System.out.println(report);
     ...