久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<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 class="qwawimqqmiuu" id='AsLfz'><tfoot id='AsLfz'></tfoot><dl id='AsLfz'><fieldset id='AsLfz'></fieldset></dl></div>
<tfoot id='AsLfz'></tfoot>

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

        讀取文本文件并存儲出現的每個字符

        Read a text file and store every single character occurrence(讀取文本文件并存儲出現的每個字符)
          <tbody id='N0HG2'></tbody>
          <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 class="qwawimqqmiuu" id='N0HG2'><tfoot id='N0HG2'></tfoot><dl id='N0HG2'><fieldset id='N0HG2'></fieldset></dl></div>
          • <bdo id='N0HG2'></bdo><ul id='N0HG2'></ul>
            • <small id='N0HG2'></small><noframes id='N0HG2'>

                1. 本文介紹了讀取文本文件并存儲出現的每個字符的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想制作一個 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用錯誤)
                  Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 語句 - 是“或/“和可能的?)
                  Java Replace Character At Specific Position Of String?(Java替換字符串特定位置的字符?)
                  What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作數的三元表達式的類型是什么?)
                  Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉換 char 原語?)
                  What#39;s the best way to check if a character is a vowel in Java?(在 Java 中檢查字符是否為元音的最佳方法是什么?)

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

                          <tbody id='5P3gz'></tbody>
                        • <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 class="qwawimqqmiuu" id='5P3gz'><tfoot id='5P3gz'></tfoot><dl id='5P3gz'><fieldset id='5P3gz'></fieldset></dl></div>
                          • 主站蜘蛛池模板: 亚洲一区二区三区在线播放 | 亚洲精品视频观看 | 美女艹b | 欧美日产国产成人免费图片 | 网址黄 | 精品日韩 | 狠狠色综合久久婷婷 | 免费特级黄毛片 | 亚洲国产精品久久久 | 国产真实精品久久二三区 | 国产综合第一页 | 中文天堂在线一区 | 久久精品国产久精国产 | 国产精品成人一区二区三区 | 狠狠草视频 | 可以在线看的黄色网址 | 日韩精品在线观看视频 | 小早川怜子xxxxaⅴ在线 | 九九热精品视频在线观看 | 精品无码久久久久久久动漫 | 欧美日韩中文字幕在线 | 日韩中文字幕一区 | 日本亚洲精品成人欧美一区 | 亚洲成人综合网站 | 在线国产小视频 | 视频一区欧美 | 久草新在线 | 欧美日韩在线观看一区 | 性视频网| 日日摸夜夜添夜夜添特色大片 | 日韩手机在线视频 | 日本涩涩网| 欧美久久久久久久久中文字幕 | 久久国产精品视频观看 | 亚洲日韩欧美一区二区在线 | 成人在线精品视频 | 亚洲欧美第一视频 | 国产精品jizz在线观看老狼 | 亚洲欧美日韩久久 | 91亚洲国产成人精品一区二三 | 欧美1—12sexvideos |