unlimited4311
unlimited4311

Reputation: 57

How to count occurrence of each character in the alphabet with a file

I would like to know how to go about starting a program that reads in a file and goes line by line counting the occurrence of each letter in the alphabet.I know I would like to use an array that holds the alphabets, but I am not sure how to go about reading each line and then printing the count out for each alphabetical occurrence.

Upvotes: 0

Views: 2649

Answers (4)

David.Jones
David.Jones

Reputation: 1411

BufferedReader reader = new BufferedReader(new FileReader("somefile.txt"));
int ch; 
Map<char, int> charCount = new Map<char, int>();
while((ch=reader.read()) != -1) {
    if(charCount.contains(ch)) {
        charCount.put(ch, charCount.get(ch)++);
    } else {
        charCount.put(ch, 1);
    }
}
reader.close();

for(String key: charCount.keySet())
    System.out.println(key + " - " + charCount.get(key));
System.out.println();

This will keep a counter for EVERY character in the file. If you want to limit this set to an alphabet, this can be done easily by checking is ch is in the alphabet before putting it in charCount.

Upvotes: 0

konomith
konomith

Reputation: 24

    public void count() throws Exception {
        int[] array = new int[26];
        BufferedReader br = new BufferedReader(new FileReader("file.txt"));
        int ch;
        while((ch = br.read()) != -1) {
            if (ch > 64 && ch < 97) // Upper case letters
                ch += 32; // to get the ascii value of the letter in lowercase
            if(-1 < ch-97 && ch-97 < 26)
                array[ch-97] += 1;
        }
        br.close();
        int i = 0;
        for (char chr = 'a'; chr <= 'z'; chr++) {
            System.out.printf("%s --> %d\n", chr, array[i]);
            i++;
        }
    }

Upvotes: 0

Nitesh
Nitesh

Reputation: 3903

try this

public void countOccurence() {
 BufferedReader reader
                     = new BufferedReader(new FileReader("somefile.txt"));

String text = "";
while((ch=reader.read()) != -1) {
       text = text +ch;
    };
    reader.close();


 for(Character a :"abcdefghijklmnopqrstuvwxyz0123456789".toCharArray())
    {

         int ch;
         char charToSearch=a;
         int counter=0;
         for(Character b : text.toCharArray())
            {
              if(charToSearch == (char)b) {
                counter++;
                  }
            }

    System.out.pritln("Character: " +charToSearch+ " occurs " + counter 
    + " times.");

         }
    }

Mark as up, if it works for you :)

Upvotes: 0

Ismael Di Vita
Ismael Di Vita

Reputation: 1836

  BufferedReader reader = new BufferedReader(new FileReader("somefile.txt"));
    int ch;
    char charToSearch='a';
    int counter=0;
    while((ch=reader.read()) != -1) {
        if(charToSearch == (char)ch) {
            counter++;
        }
    };
    reader.close();

    System.out.println(counter);

Reference: Counting letter occurrence

Upvotes: 2

Related Questions