Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java letter frequency

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java letter frequency

    Could someone give me some suggestions on how I could code a program that go through a string and determine the frequency of each letters? I can't use char so the only thing I have left is array. I was thinking of going comparing the substring of the string 1 by 1 and each time it match a letter in the alphabet array, I would make the letter counter to be ++. But this would mean I have to create 26 different int variable to keep count of the individual letters. Any ideals on how I can avoid making 26 counter variable for the letter? Thanks. (I would provide the code but the current code I have consist of the 26 variable which is not much useful)

    ** I thought of something, I will edit the post with the code in a few min.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java letter frequency

    how I can avoid making 26 counter variable
    Use an array.

    I can't use char
    Does that mean you can only work with Strings?

    Are you using java version 1.7? It allows Strings in switch statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java letter frequency

    I used your suggestion of using an array. It worked. Thanks you so much for the suggestion!

    -Can you help me figure out why percent[x] is giving me 0.00? Thanks
     int total = 0 ;
            for(int i = 0 ; i < rate.length; i++)
            {
                total += rate[i];
            }
            double [] percent = new double[26];
            System.out.println("Total is "+total);
            System.out.println("Rate at [0] is " + rate[0]);
            for(int x = 0 ; x < rate.length;x++)
            {   
                percent[x] = (rate[x] / total);          
            }
     
            System.out.println("Percent at [0] is "+percent[0]);

    Total is 52
    Rate at [0] is 7
    Percent at [0] is 0.0

    ** It work when I cast rate[] to an int but why does this occur? Isn't the percent[] is a double so wouldn't it result in a double without me casting the rate[]?

Similar Threads

  1. Java Program - Letter Frequencies - HELP!
    By kbrady481 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: March 13th, 2013, 08:29 PM
  2. Replies: 0
    Last Post: July 25th, 2012, 04:53 AM
  3. Word Frequency in News Articles
    By mblem22 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 26th, 2012, 04:37 PM
  4. [SOLVED] frequency count/Big Oh
    By edvir in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 25th, 2012, 07:24 AM
  5. Llist of words and frequency of each word
    By jkkj in forum Collections and Generics
    Replies: 7
    Last Post: August 16th, 2011, 09:59 AM