Hi, Occurrences Of Every Character In A String

Hi All,I want to write a program wherein I can check the occurrences of every character in the string given.What should I do, is there a in built function to do this. Please help
[206 byte] By [New_to_Javaa] at [2007-11-27 8:29:47]
# 1

> Hi All,

>

> I want to write a program wherein I can check the

> occurrences of every character in the string given.

>

> What should I do, is there a in built function to do

> this.

>

> Please help

There are lots of useful String functions available. You could try looking at the String api.

petes1234a at 2007-7-12 20:20:09 > top of Java-index,Java Essentials,New To Java...
# 2

> I want to write a program wherein I can check the

> occurrences of every character in the string given.

What does this mean? Do you want each character split off separately. Do you want to count how many times each character appears? Do you want to find out if a particular character appears in the String?

floundera at 2007-7-12 20:20:09 > top of Java-index,Java Essentials,New To Java...
# 3
HI, Sorry for late reply.. My computer was down.. Say If i have a string "Preview"then my program should be able to check how many p's are there and then how many r's ,and till w... i.e. how amny times each character ocuurs in the string.
New_to_Javaa at 2007-7-12 20:20:09 > top of Java-index,Java Essentials,New To Java...
# 4
OK. As suggested already the String class has method(s) you can use. To keep track of the letters and their counts, use a Map.
floundera at 2007-7-12 20:20:09 > top of Java-index,Java Essentials,New To Java...
# 5
I don't know much about collections .. I thougth I will Convert that string into an array and then compare its each elements and then check.. will this be possible and good.. Can you be more specific...
New_to_Javaa at 2007-7-12 20:20:10 > top of Java-index,Java Essentials,New To Java...
# 6
You don't have to convert to an array. Just use a loop and call charAt() inside the loop.
floundera at 2007-7-12 20:20:10 > top of Java-index,Java Essentials,New To Java...
# 7

I wrote this program and this works fine... Thanks

public class Main_Character_Occurence

{

public static void main(String args[])

{

String inputString = " Test String";

String matches = "abcdefghijklmnopqrstuvwxyz";

int sum = 0;

for (int i = 0; i < matches.length(); i++)

{

for (int ctr = 0; ctr < inputString.length(); ctr++)

{

if (matches.charAt(i) == Character.toLowerCase(inputString

.charAt(ctr)))

sum++;

}

System.out.println(matches.charAt(i) + "=" + sum);

sum = 0;

}

}

}

The output I am getting is this

a=0

b=0

c=0

d=0

e=1

f=0

g=1

h=0

i=1

j=0

k=0

l=0

m=0

n=1

o=0

p=0

q=0

r=1

s=2

t=3

u=0

v=0

w=0

x=0

y=0

z=0

New_to_Javaa at 2007-7-12 20:20:10 > top of Java-index,Java Essentials,New To Java...