<legend id='N0HG2'><style id='N0HG2'><dir id='N0HG2'><q id='N0HG2'></q></dir></style></legend>
      <tfoot id='N0HG2'></tfoot>

      <i id='N0HG2'><tr id='N0HG2'><dt id='N0HG2'><q id='N0HG2'><span id='N0HG2'><b id='N0HG2'><form id='N0HG2'><ins id='N0HG2'></ins><ul id='N0HG2'></ul><sub id='N0HG2'></sub></form><legend id='N0HG2'></legend><bdo id='N0HG2'><pre id='N0HG2'><center id='N0HG2'></center></pre></bdo></b><th id='N0HG2'></th></span></q></dt></tr></i><div id='N0HG2'><tfoot id='N0HG2'></tfoot><dl id='N0HG2'><fieldset id='N0HG2'></fieldset></dl></div>
      • <bdo id='N0HG2'></bdo><ul id='N0HG2'></ul>
    1. <small id='N0HG2'></small><noframes id='N0HG2'>

      1. 读取文本文件并存储出现的每个字符

        时间:2023-07-28

        <small id='AsLfz'></small><noframes id='AsLfz'>

        <i id='AsLfz'><tr id='AsLfz'><dt id='AsLfz'><q id='AsLfz'><span id='AsLfz'><b id='AsLfz'><form id='AsLfz'><ins id='AsLfz'></ins><ul id='AsLfz'></ul><sub id='AsLfz'></sub></form><legend id='AsLfz'></legend><bdo id='AsLfz'><pre id='AsLfz'><center id='AsLfz'></center></pre></bdo></b><th id='AsLfz'></th></span></q></dt></tr></i><div id='AsLfz'><tfoot id='AsLfz'></tfoot><dl id='AsLfz'><fieldset id='AsLfz'></fieldset></dl></div>
        <tfoot id='AsLfz'></tfoot>

                <tbody id='AsLfz'></tbody>
                  <bdo id='AsLfz'></bdo><ul id='AsLfz'></ul>
                  <legend id='AsLfz'><style id='AsLfz'><dir id='AsLfz'><q id='AsLfz'></q></dir></style></legend>

                  本文介绍了读取文本文件并存储出现的每个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I would like to make a java program that will read a text file and store every single character occurrence. So it will account for punctuation, letters, numbers,uppercase, lowercase ect. Given a text file like:

                  玫瑰是红色的,

                  紫罗兰色是蓝色的.

                  打印值如下所示:

                  R : 1

                  r : 3

                  我:1

                  , : 1

                  [等]

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

                  So far I am able to read a file and count words, lines, chars.

                  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 
                                  }
                              }
                          }
                      }
                  

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

                  I have not learned hash maps/tables or treemaps; looking for some advice on how to store all char types and their occurrences either using an array,arraylist or linkedlist.

                  推荐答案

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

                  A char is a 16-bit unsigned value, and if you cast it to an int, then you'll get a value between 0 and 65535. That means that you can just use an array to store your characters:

                  int[] charCounts = new int[65536];
                  

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

                  and then when you want to record an occurrence of char c:

                  charCounts[(int) c]++;
                  

                  当您想读取计数时:

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

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

                  There is nothing to stop you doing it with a HashMap<Character,Integer> if you want to do it as an exercise, though it's more heavyweight than it needs to be for this:

                  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());
                  

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

                  Note that for all of this I've assumed you're dealing only with printable characters. If not, you'll want to do something about that when you print them out.

                  这篇关于读取文本文件并存储出现的每个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么我需要在 byte 和 short 上显式转换 char 原语 下一篇:具有 int 和 char 操作数的三元表达式的类型是什么

                  相关文章

                  最新文章

                    1. <tfoot id='5P3gz'></tfoot>
                      <legend id='5P3gz'><style id='5P3gz'><dir id='5P3gz'><q id='5P3gz'></q></dir></style></legend>

                    2. <small id='5P3gz'></small><noframes id='5P3gz'>

                      • <bdo id='5P3gz'></bdo><ul id='5P3gz'></ul>
                      <i id='5P3gz'><tr id='5P3gz'><dt id='5P3gz'><q id='5P3gz'><span id='5P3gz'><b id='5P3gz'><form id='5P3gz'><ins id='5P3gz'></ins><ul id='5P3gz'></ul><sub id='5P3gz'></sub></form><legend id='5P3gz'></legend><bdo id='5P3gz'><pre id='5P3gz'><center id='5P3gz'></center></pre></bdo></b><th id='5P3gz'></th></span></q></dt></tr></i><div id='5P3gz'><tfoot id='5P3gz'></tfoot><dl id='5P3gz'><fieldset id='5P3gz'></fieldset></dl></div>