mercredi 6 juillet 2016

How to count the number of letter in a string and remove duplicate letter in output

Hey i am beginner in java programming and i have written a program to count the number of occurrence in string and i want to improve this code.

import java.io.*;
public class Letters
{
    public static void main(String let[]) throws IOException
    {
       InputStreamReader r = new InputStreamReader(System.in);
        BufferedReader b = new BufferedReader(r);
        System.out.println("Enter The Sentense");
        String s = b.readLine();
        String store="";
        String store1="";

        for(int i=0;i<=s.length()-1;i++)
        {
            int count =0;
            store=s.charAt(i)+"";

            for(int j=0;j<=s.length()-1;j++)
            {
                store1=s.charAt(j)+"";
                if(store.equals(store1))
                {
                    ++count;
                }
            }
            System.out.println(store+" : "+count);
        }
    }
}

And output i am getting for this is

Enter The Sentense
ddooooonnneeeeee
d : 2
d : 2
o : 5
o : 5
o : 5
o : 5
o : 5
n : 3
n : 3
n : 3
e : 6
e : 6
e : 6
e : 6
e : 6
e : 6

But i want to get my output like this

Enter The Sentense
ddooooonnneeeeee
d:2
o:5
n:3
e:6

Any suggestion how can i do it.

Aucun commentaire:

Enregistrer un commentaire