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.