Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

java - Read a text file and store every single character occurrence

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:

Roses are red,

Violets are blue.

printing the values will look like:

R : 1

r : 3

i : 1

, : 1

[ect]

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

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

charCounts[(int) c]++;

When you want to read off the counts:

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

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

and when you want to record the occurrence of char c:

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

and when you want to read off:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...