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 5 of 5

Thread: Finding frequency and probability of characters in a string

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Finding frequency and probability of characters in a string

    ***Problem****In this program if the frequency is large (say, 9) the frequency shows it as 9.000000000006.
    import java.io.*;
     
    public class Freq {
     
        public static void main(String[] args) 
          {
            System.out.println("Type string : ");
            String str = "";
            InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(input);
     
     
            try 
            {
     
                str = reader.readLine();
     
            } 
            catch (Exception e) { }
            System.out.println("String is : " + str);//part 2 string input
     
            /*finding frequency of characters*/
            double len = str.length();
            double count = 0;
            double prob = 0;
            double ref = 1000;
            double freq = 0;
            double prqueue[] = new double[96];
            char charArr[] = new char[96];
            double temp = 0;
            char flag;
     
            for (int i = 0; i < 96; i++) 
               {
                char currChar = (char) (32 + i);
                for (int k = 0; k < len; k++)
                {
                    char currStrChar = str.charAt(k);
                    if (currChar == currStrChar) {
                        count = count + (1 / ref);//this is used if the file is too large
     
                 }
     
                }
                freq = count * ref;
                prob = freq / len;
                if (freq != 0) 
               {
                    System.out.println("occurence of " + currChar + ": " + freq + " and probability is : " + prob);
                }
                prqueue[i] = prob;
                charArr[i] = currChar;
                count = 0;
     
     
            }
     
     
            System.out.println();
            System.out.println();
            System.out.println("PRIORITY QUEUE :");
            /*priority queue*/
            for (int x = 0; x < 95; x++) {
                for (int y = 0; y < 95 - x - 1; y++) {
                    if (prqueue[y] > prqueue[y + 1]) {
                        temp = prqueue[y];
                        flag = charArr[y];
                        prqueue[y] = prqueue[y + 1];
                        charArr[y] = charArr[y + 1];
                        prqueue[y + 1] = temp;
                        charArr[y + 1] = flag;
                    }
                }
            }
     
            for (int x = 0; x < 95; x++) {
                if (prqueue[x] != 0) {
                    System.out.println(charArr[x] + " has value in priority queue " + prqueue[x] + ", ");
                }
            }
        }
    }
    Last edited by copeg; October 30th, 2010 at 10:48 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,342
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Finding frequency and probability of characters in a string

    Quote Originally Posted by Aberforth View Post
    ***Problem****In this program if the frequency is large (say, 9) the frequency shows it as 9.000000000006.
    Why is that a problem? What do you expect/want to see?

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Finding frequency and probability of characters in a string

    actually the frequency should be an integer i.e,9.0000000000 and not 9.0000000006.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Finding frequency and probability of characters in a string

    actually the frequency should be an integer
    Then why are you using a double?

    db

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Finding frequency and probability of characters in a string

    i am using double because probability will have a decimal part.
    but when i am going to find the frequency , the decimal part should be entirely zero.
    for example, the string be: aaaabbbbbbbbbbbccc
    the frequency of 'b' must be 11.
    but when i run the program, the frequency is shown as 11.0000000000004.
    i am confused about the .00000000004 part.

Similar Threads

  1. finding String in a file
    By nasi in forum Java Theory & Questions
    Replies: 12
    Last Post: May 31st, 2010, 01:16 PM
  2. regular expressions, characters unallowed in file names
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:17 PM
  3. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM
  4. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 09:23 AM
  5. Certain Chinese Characters not displayed properly.
    By kerwintang in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 20th, 2009, 08:23 AM