【问题标题】:Read a text file and store every single character occurrence读取文本文件并存储出现的每个字符
【发布时间】:2014-12-19 15:54:45
【问题描述】:

我想做一个java程序来读取一个文本文件并存储每一个字符出现。所以它会考虑标点符号、字母、数字、大写、小写等。 给定一个文本文件,例如:

玫瑰是红色的,

紫罗兰色是蓝色的。

打印值如下所示:

R : 1

r : 3

我:1

, : 1

[等]

到目前为止,我能够读取文件并计算字数、行数和字符数。

package Exercise3;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;
    public class StringTokenizerDemo1
    {
        public static void main(String[] args) throws IOException
        {
            Scanner keyboard = new Scanner(System.in);
            File file = new File("C://Users//guy//Desktop//Practice.txt");
            Scanner inputFile = new Scanner(file);
            String line, word;
            StringTokenizer token;
            int words = 0; //word count 
            int lines = 0; //line count
            int chars = 0; //char count 
            while (inputFile.hasNext())
            {
                lines++; //add one to line count 
                line = inputFile.nextLine();
                token = new StringTokenizer(line, " ");
                while (token.hasMoreTokens())
                {
                    words++; //add one word count 
                    word = token.nextToken();
                    chars+= word.length(); //add to char count 
                }
            }
        }
    }

我没有学过哈希图/表或树图;寻找一些关于如何使用数组、数组列表或链接列表存储所有字符类型及其出现的建议。

【问题讨论】:

    标签: java arrays char


    【解决方案1】:

    这将计算数组中每个字符的出现次数
    公共课爵士乐{

        public static void main(String[] args) {
            String [] arr = {"a", "b", "a","c", "d"}; 
            HashMap<String, Integer> map = new HashMap<String,Integer>(); 
             for (String i : arr) {
                    if (map.get(i) == null) {
                        map.put(i, 1);
                    } else {
                        map.put(i, map.get(i) + 1);
                    }
             }
    

    【讨论】:

      【解决方案2】:

      char 是一个 16 位无符号值,如果您将其转换为 int,那么您将得到一个介于 0 和 65535 之间的值。这意味着您可以只使用一个数组来存储您的字符:

      int[] charCounts = new int[65536];
      

      然后当你想记录char c的出现时:

      charCounts[(int) c]++;
      

      当您想读取计数时:

      for (int i=0; i<65536; i++)
          if (charCounts[i]>0)
              System.out.println((char)(i)+": "+charCounts[i]);
      

      如果您想将其作为练习进行,则没有什么可以阻止您使用 HashMap&lt;Character,Integer&gt; 进行此操作,尽管它比它需要的重量更重:

      HashMap<Character,Integer> map = new HashMap<Character,Integer>();
      

      当你想记录char c的出现时:

      if (!map.containsKey(c))
          map.put(c,1);
      else
          map.put(c,map.get(c)+1);
      

      当你想阅读时:

      for (Map.Entry<Character,Integer> entry: map.entrySet())    
          System.out.println(entry.getKey()+": "+entry.getValue());
      

      请注意,对于所有这些,我假设您只处理可打印字符。如果没有,当你打印出来时,你会想要做一些事情。

      【讨论】:

      • 非常感谢,我最终使用了 HashMap。
      【解决方案3】:

      如果您只想存储有限的 nr 个字符,其中一些字符是合法的,而其他字符被忽略,您可以创建一个固定大小的数组,其中 char 的 int 值表示其索引,然后增加该索引中的出现值 (如 chiastic-security 的回答所示)。

      使用 ArrayList/LinkedList 最简单的方法可能是创建一个表示 char 及其出现的类,然后将该对象添加到列表中。

      <read char>
      <search list for char>
      <if list contains char>
         <increment char's occurence>
      <else>
         <create a new char/occurence-object and add it to the list>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多