0

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.

unlimited4311
  • 57
  • 2
  • 6
  • 1
    What do you have so far? – Paul Nov 28 '14 at 19:49
  • nothing right now, but the code that reads in the file. I'm trying to get an idea where to start – unlimited4311 Nov 28 '14 at 19:51
  • Well, there are plenty of tutorials about reading files and printing arrays and the question about finding character occurrences in a String gets asked twice a week. I guess there shouldn't be a problem finding and using that stuff. – Tom Nov 28 '14 at 19:51
  • [MCVE](http://stackoverflow.com/help/mcve) - If you don't know how to do a certain part, at least provide the parts that you know how to do. – Compass Nov 28 '14 at 19:52

4 Answers4

2
  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

Community
  • 1
  • 1
Ismael Di Vita
  • 1,806
  • 1
  • 20
  • 35
0

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 :)

Nitesh
  • 3,868
  • 1
  • 20
  • 26
0
    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++;
        }
    }
konomith
  • 24
  • 1
  • 3
0
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.

David.Jones
  • 1,413
  • 8
  • 16