Javatpoint Logo

Count Total Number Of Times Each Alphabet Appears In The String Java Program Code With Example

By: mithun*** On: Fri Dec 23 15:31:12 IST 2016     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
i written a program to count each alphabet appears in the given string and i got output but already counted alphabet is repeating again i think my doubt is not much clarity i will provide my code below and output
code:

package mithun;

public class ForLoop
{
public static void main(String[] args)
{
String s1="mithunmithun";
int count=0;
for(int i=0;i<=s1.length()-1;i++)
{
char c1=s1.charAt(i);
for(int j=0;j<=s1.length()-1;j++)
{
if(s1.charAt(i)==s1.charAt(j))
{
count=count+1;
}
}
System.out.println(s1.charAt(i)+"-"+count);
count=0;
}
}
}
output:
m-2
i-2
t-2
h-2
u-2
n-2
m-2
i-2
t-2
h-2
u-2
n-2

expected output is
m-2
i-2
t-2
h-2
u-2
n-2
Up0Down