ThatBoy123
ThatBoy123

Reputation: 77

Finding the Frequency of Every Letter in a Text File

I'm trying to print out the frequency of every letter found in the text file. I'm not sure how to loop this and properly get an array to do what I want it to. I've got the basics but I'm lost. Any ideas?

A sample text file:

Hello, my name is Zachary. Taking people's x-rays is what I do for a living.

Desired Output:

Letters - Frequencies in file:

a - 7

b - 0

c - 1

d - 1

e - 4

f - 1

g - 2

h - 3

i - 6

j - 0

k - 1

l - 4

m - 2

n - 3

o - 4

p - 2

q - 0

r - 2

s - 3

t - 2

u - 0

v - 1

w - 1

x - 1

y - 3

z - 1

/*
 * program that reads in a text file and counts the frequency of each letter
 * displays the frequencies in descending order
 */

import java.util.*; //needed for Scanner
import java.io.*;  //needed for File related classes
public class LetterCounter {
  public static void main(String args[]) throws IOException{
    Scanner keyboard = new Scanner(System.in); //Scanner to read in file name
    System.out.println("Enter the name of the text file to read:");
    String filename = keyboard.next();

    //This String has all the letters of the alphabet
    //You can use it to "look up" a character using alphabet.indexOf(...) to see what letter it is
    //0 would indicate 'a', 1 for 'b', and so on.  -1 would mean the character is not a letter
    String alphabet = "abcdefghijklmnopqrstuvwxyz";

    //TODO: create a way to keep track of the letter counts
    //I recommend an array of 26 int values, one for each letter, so 0 would be for 'a', 1 for 'b', etc.
    int[] myArray = new int[26];

    Scanner fileScan = new Scanner(new File(filename));  //another Scanner to open and read the file


    char letterFind = fileScan.nextLine().charAt(0);
//loop to read file line-by-line
    while (fileScan.hasNext()) {  //this will continue to the end of the file
      String line = fileScan.nextLine();  //get the next line of text and store it in a temporary String
      line = line.toLowerCase( ); // convert to lowercase

      //TODO: count the letters in the current line
      int letter = 0;
      for (int i=0; i<myArray.length; i++) {
        char current = line.charAt(i);
        if (letterFind == current) {
        letter++;
    }
      }
    fileScan.close(); //done with file reading...close the Scanner so the file is "closed"



    //print out frequencies
    System.out.println("Letters - Frequencies in file:");

    //TODO: print out all the letter counts
    System.out.println(line.charAt(letter));

  }
}
}

Upvotes: 0

Views: 1797

Answers (1)

radulfr
radulfr

Reputation: 616

Remember that the char type is a number in Java, so that if you have an array whose size is 26, you can increment the right element by saying myArray[line.charAt(i) - 'a']++;

Also note that the loop should be going through the letters in the line, not through the array.

When you want to print the letters, go through the array and get the correct letter from the index with the opposite calculation: if your index is in variable i, the corresponding letter is i + 'a' and its frequency is myArray[i].

Upvotes: 1

Related Questions