public class Main {
    public static void main(String[] args) {
        String string = "H e l l o ! n o w c o d e r";
        Scanner scanner= new Scanner(System.in);
        String word = scanner.next();
        scanner.close();
        System.out.println(check(string, word));
    }

    public static int check(String str, String word) {

        //write your code here......
         char c = word.charAt(0); //将第二个字符串用字符表示
        int count = 0;
        for(int i = 0; i < str.length(); i++) //遍历第一个字符串
            if(c == str.charAt(i)) //比较每个字符与c是否相同
                count++; //相同则计数
        return count;
    }
}

也有另一种方法:

        return str.length() - str.replace(word,"").length();
    }

replace() 方法通过用 newChar 字符替换字符串中出现的所有 searchChar 字符,并返回替换后的新字符串。即将word字符直接消去。用原字符长度-消去后长度=word出现次数。