Java - Write code to count character in given text

Requirements

Write code to count character in given text

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String text = "book2s.com";
        char ch = 'o';
        System.out.println(countLetter(text, ch));
    }//from  www  .j av a2s. co m

    /**
     * countLetter - counts character in given text
     * @param - text - original string
     * @param - ch - character
     * @return count of characters
     */
    public static int countLetter(String text, char ch) {
        int count = 0;
        for (int i = 0; text != null && i < text.length(); i++) {
            if (text.charAt(i) == ch)
                count++;
        }
        return count;
    }
}