Java - Write code to count char in a string

Requirements

Write code to count char in a string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        char character = 'o';
        System.out.println(count(string, character));
    }/* w  w  w .j a  v  a 2s  . co m*/

    public static int count(String string, char character) {
        int n = 0;
        for (int i = 0; i < string.length(); i++) {
            if (string.charAt(i) == character)
                n++;
        }
        return n;
    }
}