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));
    }/*from w  w  w  .j ava 2  s .c  om*/

    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;
    }
}