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