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

Thread: Help with String based program!!!

  1. #1
    Junior Member astroann's Avatar
    Join Date
    Dec 2010
    Location
    India
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Help with String based program!!!

    The Question is:
    Write a program to input a string and print the frequency of each character.
    Example:
    INPUT: COMPUTER HARDWARE
    OUTPUT: CHARACTERS FREQUENCY
    A 2
    C 1
    AND SO ON...
    The source code i wrote was:

    import java.io.*;
    class CharCount
    {
    public static void main(String args[])throws IOException
    {
    int i,h,k;
    String a="AZazAz^_^";
    int v=a.length();
    System.out.println("Character"+"\t"+"FREQUENCY");
    System.out.println(v);//here*
    for(i=65;i<123;i++)
    {h=0;
    for(k=0;k<v;k++)
    {
    if (a.charAt(k)==i)
    h++;
    }
    System.out.println(a.charAt(k) + "\t" + h);}}//#

    }

    There were no compilation errors.
    But when i ran it, the program executed till *.

    And the following errors came up:
    java.lang.StringIndexOutOfBoundsException: String index out of range:9
    at java.lang.String.charAt(String.java:686)
    at CharCount.main(CharCount.java.18)

    and the source code opened up with error in line #.

    Can some please help me with this. I m really stuck up. Urgent help neccesary. Thanks in advance.


  2. #2
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Help with String based program!!!

    here's my implementation.
    import java.io.*;
    class CharCount
    {
        public static void main(String args[]) throws IOException
        {
            int i,h,k;
            String a="bcDedAZazAz^_^";
            int v=a.length();
            char[] toChar = a.toLowerCase().toCharArray(); //get string to char array
            char[] charStore = new char[ v ]; // store characters found
            int [] charCountStore = new int[ v ]; //store count of characters found
            for(i=0;i<v;i++) { charCountStore[i]=0;} //initialize count to 0
            System.out.println("Character"+"\t"+"FREQUENCY");
            for(i=0;i< v; i++){
                if ( isInArray( charStore ,toChar[i] ) ){
                    for(h=0;h<v;h++){
                        if ( Character.valueOf( charStore[h]).equals(toChar[i]) ){
                            // increase count if found in the charStore array
                            charCountStore[h]++;
                        }
                    }
                }else{
                    charStore[i] = toChar[i];
                    charCountStore[i]++;
                }
            }
            for(i=0;i<v;i++){
                System.out.printf("%c\t\t%d\n", charStore[i],charCountStore[i] );
            }
        }
     
        public static boolean isInArray ( char[] a , char c){
            // check if character is in array, if yes, return true
            boolean found=false;
            for(int i=0;i< a.length; i++){
                if ( Character.valueOf(a[i]).equals(c) ){
                    found=true;
                    break;
                }
            }
            return found;
        }
     
    }

  3. The Following User Says Thank You to JavaHater For This Useful Post:

    astroann (December 27th, 2010)

  4. #3
    Junior Member astroann's Avatar
    Join Date
    Dec 2010
    Location
    India
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: Help with String based program!!!

    import java.io.*;
    class CharCount
    {
    public static void main(String args[])throws IOException
    {
    int i,h,k;
    String a="AZazAz^_^";
    int v=a.length();
    System.out.println("Character"+"\t"+"FREQUENCY");
    System.out.println(v);//*
    for(i=65;i<123;i++)
    {h=0;
    for(k=0;k<v;k++)
    {
    if (a.charAt(k)==i)
    h++;
    }
    System.out.println(a.charAt(k-1) + "\t" + h);}}//#

    }

    The correction was at line # itself.
    I neede to write a.chatAt(k-1).
    It was because when a loop executes, then the iteration is executed, so at that time 'k' was 9, which did not exist.
    Thats all folks!!!

    Thanks @JavaHater.

  5. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with String based program!!!

    Glad to see your problem was fixed, in the future please use the tags highlight=Java and [/highlight].
    ohai, java?

Similar Threads

  1. beginner w/ text based RPG
    By rbread80 in forum Java Theory & Questions
    Replies: 12
    Last Post: December 4th, 2010, 02:39 PM
  2. token-based processing
    By amandat in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2010, 12:05 AM
  3. Some Theory based questions
    By Bacon n' Logic in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 1st, 2010, 06:23 PM
  4. Exception-Based Problem
    By Hax007 in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 03:53 AM
  5. Com based component project using Java
    By jazz2k8 in forum Java Native Interface
    Replies: 1
    Last Post: October 7th, 2008, 10:54 PM

Tags for this Thread