Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with letter counter program

    Alright so I have my code and my methods setup on how i want the program to execute but im still having a little difficulties. The object of the program is to counts the frequencies of alphabet letters in a given file and records the letters and frequencies in another given file. The array is to save the letters and another array to save their frequencies, and a letter and its frequency should have the same index, i.e., letter 'A' has index 0 for both arrays. I have included comments.

    [CODE]
    import java.io.BufferedReader;

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;


    public class LetterCounter {

    /**
    * The size of each array is 26 as there are 26 letters in English alphabet.
    *
    * @param setOfLetter the array to save the alphabet letters
    * @param freqOfLetters the array to save frequencies of letters
    * @param maxFreqIndex the array index with the maximum frequency
    */
    static final int LENGTH = 26;
    char setOfLetters[] = new char[LENGTH];
    int freqOfLetters[] = new int[LENGTH];
    int maxFreqIndex;

    /**
    * This method return true if ch is in [a-z] or [A-Z]
    * @param ch the input character
    * @return true if ch is in the range; false otherwise
    */
    protected boolean isLetter(char ch) {

    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
    return true;
    }
    else
    {
    return false;
    }

    }

    /**
    * this method find the index of character ch in the arrays.
    * The index is calculated by Character.toUpperCase(ch) - 'A'
    *
    * @param ch the input character
    * @return the array index of character ch
    */

    protected int indexLetter(char ch) {
    for (int i = 0; i < setOfLetters.length; i++)
    if (setOfLetters[i] == ch) {
    //return ch;
    maxFreqIndex++;
    }
    return Character.toUpperCase(ch);
    }



    /**
    * This method initializes both arrays. elements in freqOfletters are set to zero,
    * and elements in setOfLetters are set to capital letters
    *
    */
    protected void initFreqs() {

    //TODO

    }

    /**
    * this method find the array index with the maximum frequency in an array
    * @param values an array of ints
    * @return the index with maximum value
    */
    protected int findIndexOfMax(int values[]) {
    return maxFreqIndex; // find the index of the character with max frequency

    //TODO

    }

    /**
    * This method reads character by character from the input file, check if each character is a letter. For letters, it
    * increments the frequencies in the array freqOfLetters.
    * <p>
    * This method also
    * @param inFileName the file to read from
    * @throws IOException
    */
    public void count(String inFileName) throws IOException {

    //TODO

    }

    /**
    * This method prints the results to the output file.
    * @param outFileName the file to write to
    * @throws IOException
    */
    public void printFrequencies(String outFileName) throws IOException {

    //TODO

    }


    /**
    * This is the driver. It instantiate a LetterCounter object and call count and printFrequencies methods.
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    if(args.length != 2){
    System.out.println("usage: " + LetterCounter.class.getSimpleName() + " input_file_name output_file_name");
    System.exit(-1);
    }
    LetterCounter c1 = new LetterCounter();
    try {
    c1.count(args[0]);
    c1.printFrequencies(args[1]);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    [CODE]

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,032
    Thanks
    36
    Thanked 495 Times in 489 Posts
    Wrong forum.
    Try asking moderator to move it to the JAVA forum.
    You probably not not get the answer you want here on the JavaScript forum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •